Having seen the answers coming out of questions like this one involving horror shows like trying to catch the NPE and dredge the mangled name out of the stack trace, I am asking this question so I can answer it.
Comments or further improvements welcome.
The nullish coalescing operator ( ?? ) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined , and otherwise returns its left-hand side operand.
operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-hand operand.
The COALESCE function returns NULL if all arguments are NULL . The following statement returns 1 because 1 is the first non-NULL argument. The following statement returns Not NULL because it is the first string argument that does not evaluate to NULL . you will get the division by zero error.
A nullish value is a value that is either null or undefined .
Like so:
case class ?:[T](x: T) { def apply(): T = x def apply[U >: Null](f: T => U): ?:[U] = if (x == null) ?:[U](null) else ?:[U](f(x)) }
And in action:
scala> val x = ?:("hel")(_ + "lo ")(_ * 2)(_ + "world")() x: java.lang.String = hello hello world scala> val x = ?:("hel")(_ + "lo ")(_ => (null: String))(_ + "world")() x: java.lang.String = null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With