0

i want to know about the expression

val videoCapture = this.videoCapture ?: return

especially,

?: return

part.

As far as i know "?" is used for let the variable can be assigned with null. However i cant really find the explanation related with "?: return" expression. I found this expression in google camera basic guide in kotlin. In case you need more code to figure it out.

 private fun captureVideo() {
    val videoCapture = this.videoCapture ?: return

    viewBinding.videoCaptureButton.isEnabled = false

    val curRecording = recording
    if (curRecording != null) {
        // Stop the current recording session.
        curRecording.stop()
        recording = null
        return
    }

If anyone knows what ?: return means, please kindly guide me. Thanks.

Luke Ha
  • 1
  • 1
  • 1
    Specifically, in your code example, it assigns `this.videoCapture` to the local variable `videoCapture` only if `this.videoCapture` does not reference `null`, otherwise it simply returns from the function (i.e., the rest of the function is "skipped"). – Slaw Jun 14 '22 at 06:28
  • @Slaw After reading the url you gave me, now i get it. I was really obsessed with the ? means that variable can be null in kotlin thanks for your info! – Luke Ha Jun 14 '22 at 06:35
  • You might also want to check out [Null safety | Kotlin](https://kotlinlang.org/docs/null-safety.html). – Slaw Jun 14 '22 at 06:41
  • @Slaw Its perfect document that resolves my question! Thanks a million. – Luke Ha Jun 14 '22 at 06:45

0 Answers0