This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Standard function for converting nullables to options?

2 replies
Robin Barooah
Joined: 2008-12-21,
User offline. Last seen 3 years 27 weeks ago.
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
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Standard function for converting nullables to options?

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
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
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-----

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland