The Elvis Operator

It took me WAY longer than it should have to realize why it's called that. Real smack yourself in the face moment. I've been trying to make an acronym out of it for a year now.

For those that don't know what it is, it's my absolute favorite new toy. It's an operator in Kotlin that handles null safety. If something might return null, you can use the Elvis operator to tell the code to use something else instead.

for example if getName() returns null in case of no name:

val name : String = getName() ?: "Has no name"

name will have the result of getName() or if it returns null then it will be "Has no name".

Hopefully it doesn't take you guys nearly as long as it did me to realize why they call it Elvis.

1 Like

:slight_smile:

(in swift you have the "Nil-coalescing operator" ?? to do this but it does not throw exceptions as you can in Kotlin).

It is counter-intuitive par excellance

The name "Elvis operator" refers to the fact that when its common notation, ?: , is viewed sideways, it resembles an emoticon of Elvis Presley with his signature hairstyle.[1]

New common word. (Which is totally obvious)
Wie kürzt man die am häufigsten gestellte Frage bei Support-Hotlines ab?

"Hasing" ?