Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Scala Syntax

dongyeonlee edited this page Nov 26, 2016 · 7 revisions

Scala's Syntax

  • *
  • 메서드 파라미터에 가변인자를 적용하기 위해 사용
  • 자바의 ...와 유사
  • 0 ~ n 개의 인자라고 생각하면 될 듯
  • 메서드 내부에서 가변인자가 Seq 로 바인딩 됨
  • def head(args: Int*): Int = args.head // Seq의 메서드인 head를 사용
  • head(1,2,3) // => 1
  • _*
  • Seq를 가변인자 메서드에 적용할 때 사용
  • String.format("%s + %s = %s", "1","2","3")
  • String.format("%s + %s = %s", Seq("1","2","3"):_*) // 1 + 2 = 3
  • -
  • import에서는 import *과 같은 의미
  • 다음과 같은 lambda에서
    • x => x + 1_ + 1로 하면
    • 여기에서 _x => x와 같은 의미
  • switch에서는 default:와 같음
    • _ : 로 표현
  • 다음과 같은 의미를 가짐
    • _+_(x, y) => x + y
    • _ * 2x => x * 2
    • _.headxs => xs.head
    • _ drop _(xs, n) => xs.drop(n)
Clone this wiki locally