In Code A1 I have used the let
statement, so I think it will not be null with filenameofVideo.path
But I get the following error, why?
Smart cast to 'File' is impossible, because 'filenameofVideo' is a mutable property that could have been changed by this time
At this moment, I have to use Code A2.
Code A1
private var filenameofVideo :File?=null
filenameofVideo?.let {
Navigation.findNavController(requireActivity(), R.id.fragment_container)
.navigate(UIFragmentCameraDirections.actionCameraToVideo(filenameofVideo.path))
}
Code A2
private var filenameofVideo :File?=null
filenameofVideo?.let {filenameofVideo ->
Navigation.findNavController(requireActivity(), R.id.fragment_container)
.navigate(UIFragmentCameraDirections.actionCameraToVideo(filenameofVideo.path))
}
And more, I find both Code B1 and Code B2 are correct. Why is Code B1 correct and Code A1 wrong?
Code B1
private val aa:String?=null
aa?.let {
print(aa)
}
Code B2
private val aa:String?=null
aa?.let{aa->
print(aa)
}
Added Content:
1: In Code C, the var aa might have been changed (perhaps by another thread) between the moment it is accessed in the ?.let call and the moment it is accessed within the let block.
Code C will be launched when aa is not null, and Code C will not launched when aa is null, right?
2: In Code D (I assume the compiler accept it ), the function always be launched no matter aa is null or not, it can not be accepted, so the system will interrupt, right?
Code C
private var aa: String? = null
aa?.let { kk ->
print(kk.length)
}
Code D
private var aa: String? = null
aa?.let {
print(aa.length)
}