Thunk

  • scala는 기본적으로 call by value.
  • 즉, parameter가 넘어갈 때, evaluation이 되어 넘어간다.
  • 하지만 명시적으로 call by name 으로 하고 싶을 경우, thunk를 이용.
  • def test( x : => Int ) 와 같이 사용.
def hello(): Int = {
  println(“hello”)
  1
}

def hi(s : Int): Int = {
  1
}

def hi2(s : => Int): Int = {
  1
}

hi(hello())
//hello
//res0: Int = 1

hi2(hello()) //res1: Int = 1

results matching ""

    No results matching ""