스칼라에서는 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:
| default | type $T$ |
|---|---|
0 | Int or one of its subrange types |
0L | Long |
0.0f | Float |
0.0d | Double |
false | Boolean |
() | Unit |
null | all other types |
'scala' 카테고리의 다른 글
| [scala] 콘솔에서 코드 붙여서 테스트하기 (paste) (0) | 2017.07.21 |
|---|---|
| [scala] DynamicVariable 예제 (0) | 2017.07.07 |
| [scala] 변수를 사용하기 위한 underscore (0) | 2017.07.05 |
| [scala] Church Rosser 이론 (Lambda calculus)에 대한 링크 펌질 (0) | 2017.07.04 |
| [scala] Stream 의 concat은 #::이다. (0) | 2017.07.03 |
| [scala] forall, for 예제 코드 (0) | 2017.06.30 |



댓글을 달아 주세요