- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Standard function for converting nullables to options?
Sat, 2009-02-14, 00:21
Hi,
Is there a common function somewhere that does the following?
def nullable[T] (value:T) :Option[T] = if (value == null) None else Some(value)
I find this handy for defusing calls to java, presumably people do it all the time, and I'd hate to be making my own convention if there is one already in use
--
-Robin
Robin Barooah
http://www.sublime.org
Is there a common function somewhere that does the following?
def nullable[T] (value:T) :Option[T] = if (value == null) None else Some(value)
I find this handy for defusing calls to java, presumably people do it all the time, and I'd hate to be making my own convention if there is one already in use
--
-Robin
Robin Barooah
http://www.sublime.org
Sat, 2009-02-14, 04:37
#2
Re: Standard function for converting nullables to options?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Careful (and even avoid) declaring that function implicit.
Paul Phillips wrote:
> On Fri, Feb 13, 2009 at 03:20:55PM -0800, Robin wrote:
>> def nullable[T] (value:T) :Option[T] = if (value == null) None else
>> Some(value)
>
> It's call onull in scalaz, so I call it that too:
>
> implicit def onull[A](a: A) = if(a == null) None else Some(a)
>
- --
Tony Morris
http://tmorris.net/
S, K and I ought to be enough for anybody.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iEYEARECAAYFAkmWOaEACgkQmnpgrYe6r60dzwCgn0+Jj/f5RS5NSohVUZWsTHtq
LIAAn2dyY6/FWyGERKOyy8xJ7gmqEd39
=DWCN
-----END PGP SIGNATURE-----
On Fri, Feb 13, 2009 at 03:20:55PM -0800, Robin wrote:
> def nullable[T] (value:T) :Option[T] = if (value == null) None else
> Some(value)
It's call onull in scalaz, so I call it that too:
implicit def onull[A](a: A) = if(a == null) None else Some(a)