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

default value for Option[T] var

2 replies
Erkki Lindpere
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.

Currently the default value for a variable of type Option[T] is null
like for any other AnyRef. Wouldn't it be better if it was None?

scala> object x {
| var n: Option[String] = _
| }
defined module x

scala> x.n
res0: Option[String] = null

ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
Re: default value for Option[T] var
"Not initialized yet" is a distinct concept from, say, "failed to find something"--not initialized is "didn't even try".  I hope there aren't many uses which rely upon this distinction, but making that change would break them.

More importantly, given that null is allowed in Scala at all, I'm a little wary of having "uninitialized" objects be anything other than null, and of having objects behave inconsistently just because one happens to be called Option.  (In particular, it means that if I wrote my own Option, it wouldn't be a drop-in replacement because its behavior w.r.t. null would change.)

  --Rex

On Tue, Feb 9, 2010 at 1:16 PM, Erkki Lindpere <erkki [at] lap [dot] ee> wrote:
Currently the default value for a variable of type Option[T] is null like for any other AnyRef. Wouldn't it be better if it was None?

scala> object x {
   | var n: Option[String] = _
   | }
defined module x

scala> x.n
res0: Option[String] = null


odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: default value for Option[T] var

On Tue, Feb 9, 2010 at 7:16 PM, Erkki Lindpere wrote:
> Currently the default value for a variable of type Option[T] is null like
> for any other AnyRef. Wouldn't it be better if it was None?
>
I'd prefer if it was ``undefined'', meaning a dereference gives a
runtime error. Once we have NotNull types we can make Option a NotNull
type and get that behavior.

Cheers

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