Questions tagged [elvis-operator]

The Elvis operator is a variant of the conditional operator with no expression between `?` and `:`. exp1 ?: exp2 is equivalent to exp1 ? exp1 : exp2.

The Elvis operator is a variation of the conditional (AKA ternary) operator that can be used when the value to be returned is the same as the condition expression when it's truthy. It's written using the same ?: notation, except the expression between ? and : is omitted.

Thus,

condition ?: expression

is equivalent to

condition ? condition : expression

except that condition will only be evaluated once.

In some programming languages the short-circuiting || operator serves this purpose. But in PHP, || always converts its result to a boolean, while the elvis operator returns the actual value of the selected operand.

PHP documentation

21 questions
551
votes
14 answers

PHP short-ternary ("Elvis") operator vs null coalescing operator

Can someone explain the differences between ternary operator shorthand (?:) and null coalescing operator (??) in PHP? When do they behave differently and when in the same way (if that even happens)? $a ?: $b VS. $a ?? $b
2
votes
1 answer

Elvis operator doesn't work in Kotlin while the synthax seems correct

I am learning Kotlin from this YouTube video and at 35:45 he is running this code: I've tried to run exactly the same code: fun main() { val x = readLine()?:"1" val y = readLine()?:"1" val z = x.toInt() + y.toInt() print(z) } But I…
1
vote
1 answer

Elvis operator and type casting precedence in Groovy

Let's take the following simple expression: ((Double) null ?: 0).getClass() Results: Groovy 3: class java.lang.Double Groovy 4: class java.lang.Integer Does anyone know the reason for the different behaviour? I'd say Groovy 4 is correct since the…
1
vote
1 answer

Regular expression to find repetitions of arbitrary characters

I have a large PHP codebase with many instances of this pattern: $result = $expression? $expression: $alternate; Which I want to replace with: $result = $expression ?: $alternate; Here $expression can be anything like $this->system->cache('1234').…
Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
1
vote
3 answers

Kotlin and ?.let: how to choose variants of variable's fields?

I have a class: class Clazz( val name: String?, val value0: String, val value1: String?, val value2: String ) I want to print value0 if we have non-null name. But if name is null, then i want return value1 only if it is not null.…
Viktor Fridman
  • 57
  • 2
  • 10
1
vote
1 answer

The Difference {}, () in kotlin null check

I'm practicing null checking in kotlin When I use Elvis operator ?: I use {} but the correct way is using (). But My mistake, the result was wrong. My Mistake Codes are below. var name: String = "eunno"//NonNull val lastName: String? = null val…
1
vote
3 answers

Kotlin String Concatenation using elvis operator

System.out.println(student != null ? student.name != null ? student.name + " is my mame" : "Name is Null" : "Student is Null "); I want to concatenate strings if the value is not null. I can do it in java. How to do it in kotlin?
Siddhant
  • 25
  • 5
1
vote
1 answer

I am not able make use of elvis operator in Kotlin

I am not able to use ?: operator in Kotlin. It still doesn't make sense to me how and when should we use it. Can anyone please let me know about it?
user16681535
1
vote
1 answer

Why mapping nullable collection is always not null in dart?

1 factory SuggestSessionResult.fromJson(Map json) { 2 final String? error = json['error']; 3 final List>? items = json['items']; 4 return SuggestSessionResult( 5 items 6 …
IL_Agent
  • 707
  • 3
  • 16
1
vote
2 answers

How to function chain instead of if else on a variable?

Hi I'm wondering if there is a nicer way to write this line of code using scoped functions instead of if else. I want to chain the .addTOBackStack() function depending on my addToStack variable if(addToStack){ supportFragmentManager …
Jisoo
  • 43
  • 6
0
votes
1 answer

Using elvis/ternary operator as an argument to a function

Let's say I have this block of code in Groovy: String x String y = "hello" def func(String str){ println(str) } func(x ?: y) Is there anything wrong with using Elvis operator as an argument to a function like I did above? I tested it and it…
anechkayf
  • 481
  • 1
  • 4
  • 12
0
votes
1 answer

Overriding a Jenkinsfile's default password parameter

I have declarative parametrized pipeline from SCM, something like this: parameters { string(name: 'USER', defaultValue: params.USER ?:'default_value', trim: true) password(name: 'PASSWORD', defaultValue: params.PASSWORD ?:'default_value') <…
i8z
  • 3
  • 2
0
votes
2 answers

Are there any default value tricks / Elvis Operator in Ocaml?

Are there any elvis like operator in Ocaml ? Any sort of optional chaining that return the right value when the left one is empty, a default value operator, like the |> operator with opposite effect. If not what are the good practices ? As an…
0
votes
1 answer

Why does the Elvis operator/null coalescing operator work this way in Kotlin?

I have the following code: companion object { private const val DEFAULT_LANGUAGE_CODE = "en-us" } val currentLanguageCode: String get() { return selectedLanguage?.code ?: configManager.get().agency?.language ?:…
Matt Wolfe
  • 8,924
  • 8
  • 60
  • 77
0
votes
0 answers

Kotlin elvis operator vs "... == true" boolean expression check preformance

Assume we have nullable val a: Boolean?. In code we want to assign value of a to another non-nullable variable val b: Boolean. If a is true then we want b also to be true If a is false or null the we want b to be false. We can do this in 2 ways: b =…
1
2