Difference between underscore initialization of var and val

后端 未结 2 819
执念已碎
执念已碎 2021-02-06 00:59

Why does val x: Int = _ not compile but var x: Int = _ does?

I\'m getting error: unbound placeholder parameter.

2条回答
  •  猫巷女王i
    2021-02-06 01:28

    You can't reassign a vals value, so you cannot use _ (init with a default value for prescribed type) with it. But you can reassign a vars value, so it's a logical to initialize it with some default value, like in Java

提交回复
热议问题