【发布时间】:2017-07-22 13:33:19
【问题描述】:
我是 Scala 的新手,此刻我正在学习它...我正在测试代码,我有一个这样的列表[Any]:
var list: List[Any] = List(
"a string",
732, // an integer
'c', // a character
true, // a boolean value
(x: String) => "String return " + x)
我想得到索引 4 的返回值,但是我得到一个错误。我不知道如何获得返回值。这是我的疑问。
var test = list(4);
test("hello") // Error
【问题讨论】:
-
错误是什么?这很重要。
-
您可能需要将函数转换为函数,因为就编译器而言,
test是如果类型为Any,它不能保证是可调用的。