35

Is there a clean way in Kotlin to assign a value to a variable only if the value is not null?

My example is:

if(x != null)
   y = x

I found a solution like

y = x? : return

but I don't understand if this does what I want and how this operator works.

2
  • If you need to set property it'possible redefine default setter, for example var y: Int? = 0 set(x: Int?) { if (x != null) field = x } Commented May 15, 2018 at 8:21
  • @user8320224 problem with that solution is y is probably a non-null field, hence the nullability check before assignment. Commented May 15, 2018 at 19:28

4 Answers 4

67

Another solution if You don't want to return from function just yet:

x?.let{ y = it }

Which checks if x is non-null then passes it as the only parameter to the lambda block.

This is also a safe call in case your x is a var.

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be the kotlin way. But i think my answer is easier to read (stackoverflow.com/a/65930287/624814).
15

The operator is called Elvis Operator. It evaluates if x is not null and if that's true, assigns x to y. If it is null, it evaluates the statement after the question mark, returning immediately and therefore leaving y untouched.

5 Comments

Sorry, the solution was "y = x ?: return" , not x = y ?: return. This solution worked for me "y = x? : return". Now I understand the Elvis operator thanks to your description. Can you correct your answer for the correct solution?... My mistake.
This will work in some scenarios, but with the question worded as is, this will not always be an appropriate solution. It also returns from the current function if x is null, so it does more than just only assigning x to y if x is not not null. If there was more code in the function after this line, that code would be skipped if x was null since the function already returned.
Why is iit called the Elvis operator?
Just watch a video of Elvis dance. Seriously. That is the reason.
@Kramer - because, if you look at the ?: sideways, it's supposed to look like the signature hairstyle of Elvis Presley.
12

For those searching and want a solution that does not immediately return. You can hold your nose and do this:

y = x ?: y

As a good programmer I shudder to assign a variable to itself just to satisfy syntax. But that's kotlin for you! Those designers thought of everything!

3 Comments

That the simple and cleanest way to do it I found to. If you find another way to do it I would like to know it.
It's acceptable for local variables but I would stay away from it when y is a property: if it has custom getter or setter those will be invoked while reassigning to itself.
@Pawel Yes, that's yet another reason to hold your nose while doing this. So many hidden potential side effects in kotlin.
6

Your code is the exact same but with less writing as:

if(x != null)
    y = x
else
    return

When using the Elvis Operator it shortens the if else statement to:

y=x ?: return

If the left side is true (x is not null) then y will be assigned, else the right side will be executed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.