스칼라에서는 underscore를 많은 용도로 사용할 수 있다.


그 중 변수를 기본값으로 초기화를 하라라는 의미이다. 


scala>  var a: String = _

a: String = null



var에 underscore를 사용할 수 있지만, val에 사용하면 에러를 리턴한다. 



scala>  val a: String = _

<console>:11: error: unbound placeholder parameter

        val a: String = _

                        ^


함수도 초기화할 수 있다.


private var myExpr: () => Int = _






scala 언어 스펙에 따르면.. 다음과 같이 정의하고 있다.

http://www.scala-lang.org/files/archive/spec/2.11/06-basic-declarations-and-definitions.html




A variable definition var $x$: $T$ = _ can appear only as a member of a template. It introduces a mutable field with type $T$ and a default initial value. The default value depends on the type $T$ as follows:

defaulttype $T$
0Int or one of its subrange types
0LLong
0.0fFloat
0.0dDouble
falseBoolean
()Unit
nullall other types



Posted by '김용환'
,