- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
null "or" default (implicit banzai..)
Tue, 2009-03-17, 20:24
Hey folks,
can someone please help me understand, why the following implicit
isn't tried? It's obviously working when being applied explicitely..
Just a slap or a pointer will probably be enough. Or even better, some
(probably) pre-existing solution to that problem..
(I'm using java libs and so sick of the visual bloat of if (x != null)
x else . Options aren't easier on the eyes either. I didn't
want to use "or" as it's too obvious of course:) Imho there should
also be a Null2Boolean (= false) but that's probably just the old C
spirit creeping to surface)
TIA,
-Martin
object Nullinger {
type T2 = { def |||(alternative : Any) : Any }
implicit def toNullinger[T1](x:T1):T2 = {
if (x == null)
new { def |||(alternative: Any) = alternative }
else
new { def |||(alternative: Any) = x }
}
}
object NullingerBanzai extends Application {
import Nullinger._
println(1 ||| 2)
println("bla" ||| "blubb")
println(false ||| true)
println(toNullinger(null) ||| "null!") // <-- works fine
println(null ||| "null!") // <--- value ||| is not a member of Null
// why isn't toNullinger used there?
}
Tue, 2009-03-17, 21:57
#2
Re: null "or" default (implicit banzai..)
You should also be aware of the fact that Boolean in scala is not nullable, but if you really want you can....
scala> val b: Boolean = null <console>:4: error: type mismatch; found : Null(null) required: Boolean val b: Boolean = null ^
scala> implicit def null2false(n: Null) = false null2false: (Null)Boolean
scala> val b: Boolean = null b: Boolean = false
On Tue, Mar 17, 2009 at 9:39 PM, Szymon Jachim <sjachim@gmail.com> wrote:
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
scala> val b: Boolean = null <console>:4: error: type mismatch; found : Null(null) required: Boolean val b: Boolean = null ^
scala> implicit def null2false(n: Null) = false null2false: (Null)Boolean
scala> val b: Boolean = null b: Boolean = false
On Tue, Mar 17, 2009 at 9:39 PM, Szymon Jachim <sjachim@gmail.com> wrote:
You might want to take a look at this:http://www.codecommit.com/blog/scala/implementing-groovys-elvis-operator-in-scala
On Tue, Mar 17, 2009 at 8:18 PM, Martin S. Weber <martin.weber@nist.gov> wrote:Hey folks,
can someone please help me understand, why the following implicit isn't tried? It's obviously working when being applied explicitely.. Just a slap or a pointer will probably be enough. Or even better, some (probably) pre-existing solution to that problem..
(I'm using java libs and so sick of the visual bloat of if (x != null) x else <whatever>. Options aren't easier on the eyes either. I didn't want to use "or" as it's too obvious of course:) Imho there should also be a Null2Boolean (= false) but that's probably just the old C spirit creeping to surface)
TIA,
-Martin
object Nullinger {
type T2 = { def |||(alternative : Any) : Any }
implicit def toNullinger[T1](x:T1):T2 = {
if (x == null)
new { def |||(alternative: Any) = alternative }
else
new { def |||(alternative: Any) = x }
}
}
object NullingerBanzai extends Application {
import Nullinger._
println(1 ||| 2)
println("bla" ||| "blubb")
println(false ||| true)
println(toNullinger(null) ||| "null!") // <-- works fine
println(null ||| "null!") // <--- value ||| is not a member of Null
// why isn't toNullinger used there?
}
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ
Tue, 2009-03-17, 22:37
#3
Re: null "or" default (implicit banzai..)
Quoting Szymon Jachim :
> You should also be aware of the fact that Boolean in scala is not nullable,
> but if you really want you can....
> (...)
> scala> implicit def null2false(n: Null) = false
> null2false: (Null)Boolean
Yeah .. I rather meant .. this being part of Predef. I'll have to
think some more about the other reply.. thanks guys.
-Martin
Tue, 2009-03-17, 23:57
#4
Re: null "or" default (implicit banzai..)
> Quoting Szymon Jachim :
>
>> You should also be aware of the fact that Boolean in scala is not nullable,
>> but if you really want you can....
>> (...)
>> scala> implicit def null2false(n: Null) = false
>> null2false: (Null)Boolean
Actually this kind of falls into the same trap as my implicit
attempt.. now I gotta understand why it has to carry the type
annotation (but the mentioned article seems to explain, still have to
get around to read it)
> implicit def null2bool[T >: Null](n: T) = if (n == null) false else true
null2bool: [T >: Null](T)Boolean
scala> val n = null
n: Null = null
> val b: Boolean = null
b: Boolean = false
> val b: Boolean = 1
b: Boolean = true
> if (n) println("banzai") if (n) println("banzai") else println("ayaken")
ayaken
buut:
> if (!n) println("banzai")
:7: error: value unary_! is not a member of Null
Sigh. back to square one...
-Martin
On Tue, Mar 17, 2009 at 8:18 PM, Martin S. Weber <martin.weber@nist.gov> wrote:
--
ʎɐqǝ uo pɹɐoqʎǝʞ ɐ ʎnq ı ǝɯıʇ ʇsɐן ǝɥʇ sı sıɥʇ