Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么初始化需要对象类型:
为什么编译:
var imp : String = _
但这不是:
var imp = _
有错误:unbound placeholder parameter
unbound placeholder parameter
是因为在初始化时不知道对象类型吗?
当您编写 时var imp : String = _,Scala 知道这imp是一个字符串,即使它没有值(它会将其初始化为null)。
imp
null
当你写的时候var imp = _,Scala 不知道类型是什么。如果您明确指定它或给出可以推断的初始值,它只能知道预期的类型。
是的——这是因为在声明/初始化时对象类型是未知的。