0
我工作的一些實際問題和困惑的超級如何返回「常量」斯卡拉如何返回恆
//Complete the following definition, so that "constant5" is a function that returns 5
// whenever it is invoked.
val constant5 :() => Int = {
}
//Complete the following definition, so that "constant" is a function that when
// invoked with integer n returns a function that returns n whenever it is invoked.
val constant : Int =>() => Int = {
}
這裏是他們將如何被調用的例子
assert ({
val r1 : Int = constant5()
val r2 : Int = constant5()
(r1, r2)
} === (5, 5)
)
assert ({
val k1 :() => Int = constant (1)
val k2 :() => Int = constant (2)
val r1 : Int = k1()
val r2 : Int = k1()
val r3 : Int = k2()
val r4 : Int = k2()
val r5 : Int = k2()
val r6 : Int = k1()
(r1, r2, r3, r4, r5, r6)
} === (1, 1, 2, 2, 2, 1)
)
我有絕對不知道從哪裏開始。 我認爲新的Int(5)會解決第一個問題,但是你不能實例化Int類。