Skip to content

?? operator does not allow 'return' or 'throw' #2095

Closed
@imrhk

Description

@imrhk

In Kotlin, I have been used to arg ?: return to check if function argument is null or some other variable is null. If that's the case, return immediately. In android this was really helpful where I did context ?: return in Android apps.

I tried doing the same with ?? operator but I wasn't successful.
Like for ScrollController? _scrollController data member, I want to do
final scrollController = _scrollController ?? return

This basically means if variable is null, return or set the value as final where object is non-null.

As far as the feature goes, it works for throws as well. Ref: https://kotlinlang.org/docs/null-safety.html#elvis-operator

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions