[scala] =과 함수 선언

scala 2016. 3. 23. 19:02

scala 2.11.8에서 테스트한 내용이다. 리턴 값이 이상하거나 명확한 리턴값이 없으면 warning이 발생한다.



아래 scala 코드는 Unit을 리턴하지만, 리턴값을 정한 케이스이다. 동작 안된다.

scala> def f():Unit = "this"

<console>:12: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses

       def f():Unit = "this"

                      ^

f: ()Unit


scala> f()

아무것도 출력 안됨




아래 scala 코드는 암묵적인 Unit을 사용했고 =를 안썼지만, 역시 동일하게 동작이 되지 않는다. 

scala> def h() { "this" }

<console>:12: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses

       def h() { "this" }

                 ^

h: ()Unit


scala> h()

아무것도 출력 안됨




아래 scala 코드는 동작 된다. 묵시적으로 함수가 String을 리턴한다. 


scala> def f() = "this"

f: ()String


scala> f()

res11: String = this



아래 scala 코드는 명시적으로 함수가 String을 리턴하는 코드라서 동작한다. 

scala> def h():String = { "this" }

h: ()String


scala> h()

res13: String = this




Posted by '김용환'
,