Just curious: In Kotlin, I would love to get some val that can be initialized by lazy, but with a parameter. That's because I need something that's created very late in order to initialize it.
Specifically, I wish I had:
private lateinit val controlObj:SomeView
或者:
private val controlObj:SomeView by lazy { view:View->view.findViewById(...)}
and then:
override fun onCreateView(....) {
val view = inflate(....)
controlObj = view.findViewById(...)
or in the 2nd case controlObj.initWith(view)
or something like that:
return view
我不能使用by lazy
,因为by lazy
不接受初始化时使用的外部参数.在本例中,包含view
的.
Of course I have lateinit var
but it would be nice if I could make sure it becomes read only after setting and I could do it in one line.
有没有一种非常干净的方法来创建一个只读变量,该变量只初始化一次,但只有在生成其他变量时才初始化?有init once
个关键词吗?在初始化之后,编译器知道它是不可变的?
I am aware of the potential concurrency issues here but if I dare to access it before init, I surely deserve to be thrown美元.