-2
只是出于好奇是有斯卡拉速记声明和返回变量
test = "test"
test
速记我想类似的东西
(test = "test")
或
//if ((test = "test") != null) test
if (test = "test") test
//编辑:我必须在返回之前设置测试变量。
只是出于好奇是有斯卡拉速记声明和返回变量
test = "test"
test
速记我想类似的东西
(test = "test")
或
//if ((test = "test") != null) test
if (test = "test") test
//编辑:我必须在返回之前设置测试变量。
根据https://stackoverflow.com/a/8845459/3641692它是不可能
我必须这样做,在多行:
var test: String = null
def setter(t:String): String = {
test = t
test
}
你的例子没有多大意义。如果你在某个范围内,那个范围内的vars/vals/defs不能从该范围外访问 - 所以不需要这个。您只需返回值(使用隐式返回)。
"test"
你为什么做了'var'?你能给出你想要完成的更大范例吗? – Daenyth 2014-11-14 17:19:30
只要你避免显式的return语句,这段代码的简写就是''test''。 – toniedzwiedz 2014-11-14 17:21:50
你通常不需要在scala中“返回”,而且通常你应该避免使用它。只要有''test''是你的函数的最后一个陈述。 – lmm 2014-11-15 10:58:53