3

I was trying Elvis operator in Kotlin code in my application and received following warning:

Unexpected tokens (use ; to seperate expressions on the same line)

Code:

    var key: String = "KEY is"
    /* "check" is name of String variable which can be null
    Warning coming on following statement*/
    var str : String = check?key.replace("KEY", "ABDS-ASDSA-DSSS")?:check

Any ideas how to resolve this?

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
  • Why do you want to use the Elvis operator? None of your variables are nullable, so it is pointless. Also, you never initialize `check` – Salem Dec 04 '17 at 19:14
  • Possible duplicate of [Kotlin Ternary Conditional Operator](https://stackoverflow.com/questions/16336500/kotlin-ternary-conditional-operator) – Neeraj Sewani Mar 04 '19 at 18:32

2 Answers2

2

I would not understand why you need any null safe operators here. None of your variables are nullable.


You wrote

check?key.replace("KEY", "ABDS-ASDSA-DSSS")?:check

? (after check) is not an operator in Kotlin.

You may have wanted the ternary conditional operator, which is simply replaced by if/else.

Judging from your comment, you seem to want the safe call operator though, which is ?., not ?.

check?.key?.replace("KEY", "ABDS-ASDSA-DSSS") ?: check

There is a difference between the Elvis operator and the safe call operator. The Elvis operator works with an expression while the safe call operator is simply a null-safe property access or method call.

The Elvis operator returns its first operand if it is not null, and returns the second operand otherwise.

The ?. operator returns null if the receiver is null, otherwise, it returns the result of the call.

Salem
  • 13,516
  • 4
  • 51
  • 70
  • Thank you. Actually the actual code differs from the code in question. Can you edit the question? – Malwinder Singh Dec 04 '17 at 19:14
  • @MalwinderSingh what do you mean? If there is something missing in the question only you can add it - you are the asker and I do not know what you wish to add – Salem Dec 04 '17 at 19:15
  • I was asking about what changes need to be made in question to clarify the problem. – Malwinder Singh Dec 04 '17 at 19:16
  • @MalwinderSingh ah - what are you trying to accomplish with `check` and `key`? What do you need `?:` for? – Salem Dec 04 '17 at 19:17
0

kotlin does not support ternary operator.

and even check is not boolean variable in your case make it boolean or use like

var check: String = "something here"
 var key: String = "KEY is"
/*Warning coming on following statement*/
 var str : String = key.replace("KEY", "ABDS-ASDSA-DSSS")?:check
Shankar
  • 2,890
  • 3
  • 25
  • 40
  • Thanks @Irony Stack. Actually I was trying to use Elvis operator of Kotlin (https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator). Can you tell me why is Elvis operator not working? – Malwinder Singh Dec 04 '17 at 19:01
  • but key is not a property of `check` you can not use it like that – Shankar Dec 04 '17 at 19:09
  • @MalwinderSingh you misunderstood the concept of elvis operator, in this statement `val l = b?.length ?: -1` 2nd operator `?:` just before -1 is elvice operator not the one before .length – Shankar Dec 04 '17 at 19:17
  • I tried your solution. It is showing warning: `Elvis operator (?:) always returns the left operand of non-nullable type String` – Malwinder Singh Dec 04 '17 at 19:23