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

Option and NotNull

9 replies
Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
I haven't dug into this much, so I could be completely off base, but why doesn't Option extend NotNull?
Here's an interpreter sessions showing usage of NotNull for reference.  It seems to me that having a null where one would expect an Option is just plain evil, and if there's compiler support that can be enlisted to prevent it, it should be enlisted.
scala> def foo(x: String with NotNull) = "Hello, " + x + "!"foo: (String with NotNull)java.lang.String
scala> foo(null)<console>:6: error: type mismatch;  found   : Null(null) required: String with NotNull       foo(null)           ^
scala> val x = "world"x: java.lang.String = world
scala> foo(x)<console>:7: error: type mismatch; found   : java.lang.String required: String with NotNull       foo(x)           ^
scala> val y: String with NotNull = "world"y: String with NotNull = world
scala> foo(y)res2: java.lang.String = Hello, world!
scala> def bar(x: String) = "Hello, " + x + "!"bar: (String)java.lang.String
scala> bar(null)res3: java.lang.String = Hello, null!
scala> foo("world")res4: java.lang.String = Hello, world!
scala> 
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: Option and NotNull

On 21/11/2009 13:53, Erik Engbrecht wrote:
> I haven't dug into this much, so I could be completely off base, but why
> doesn't Option extend NotNull?

I believe that it was already discussed in (a, don't remember which)
mailing list and that some corner cases make that modification rather
disruptive for existing code. Or impossible.

A mailing list archive digging may be helpful here.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Option and NotNull
There's a lovely boundary condition when fetching i.e. the 5th item from a list
None => The list doesn't have a 5th item
Some(xxx) => The 5th item exists and contains the value xxx Some(null) => The 5th item exists and was explicitly populated with null

On Sat, Nov 21, 2009 at 1:25 PM, Francois <fanf42 [at] gmail [dot] com> wrote:
On 21/11/2009 13:53, Erik Engbrecht wrote:
I haven't dug into this much, so I could be completely off base, but why
doesn't Option extend NotNull?

I believe that it was already discussed in (a, don't remember which) mailing list and that some corner cases make that modification rather disruptive for existing code. Or impossible.

A mailing list archive digging may be helpful here.


Erik Engbrecht
Joined: 2008-12-19,
User offline. Last seen 3 years 18 weeks ago.
Re: Option and NotNull
That's different than what I'm suggesting.  That would be:
trait Option[+T <: NotNull]
so that object contained by the Option can't be null.  What I'm suggesting is:
trait Option[+T] with NotNull
so that the compiler will enforce that a reference to an Option is not null.  The option would still be allowed to contain null.
On Sat, Nov 21, 2009 at 8:42 AM, Kevin Wright <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
There's a lovely boundary condition when fetching i.e. the 5th item from a list
None => The list doesn't have a 5th item
Some(xxx) => The 5th item exists and contains the value xxx Some(null) => The 5th item exists and was explicitly populated with null

On Sat, Nov 21, 2009 at 1:25 PM, Francois <fanf42 [at] gmail [dot] com> wrote:
On 21/11/2009 13:53, Erik Engbrecht wrote:
I haven't dug into this much, so I could be completely off base, but why
doesn't Option extend NotNull?

I believe that it was already discussed in (a, don't remember which) mailing list and that some corner cases make that modification rather disruptive for existing code. Or impossible.

A mailing list archive digging may be helpful here.


odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Option and NotNull

NotNull is very much work in progress (or should I say: in
suspension?). We'll get back to it sometimes after 2.8 is released.

Cheers

David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Option and NotNull

It's sad that this has such a low priority; about 50% of the bugs I get when
doing Java inter-operation come from NPE's, or the lack thereof as the case
may be.

I would personally really have preferred if there wasn't a null in Scala at
all, and if Java methods would have thrown a NullException when they return
null (The null check for the exception throw would be optimized away for the
case when a special method "toOption(javaMethod())" (or similarly named) was
used)

On Saturday 21 November 2009 17:55:51 martin odersky wrote:
> NotNull is very much work in progress (or should I say: in
> suspension?). We'll get back to it sometimes after 2.8 is released.
>
> Cheers
>

Jesper Nordenberg
Joined: 2008-12-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Option and NotNull

Martin just wrote it will be worked on after the 2.8 release, which is
certainly more important than NotNull at this time. But I agree that it
should be fixed in a not too distant future.

/Jesper Nordenberg

David Flemström wrote:
> It's sad that this has such a low priority; about 50% of the bugs I get when
> doing Java inter-operation come from NPE's, or the lack thereof as the case
> may be.
>
> I would personally really have preferred if there wasn't a null in Scala at
> all, and if Java methods would have thrown a NullException when they return
> null (The null check for the exception throw would be optimized away for the
> case when a special method "toOption(javaMethod())" (or similarly named) was
> used)
>
> On Saturday 21 November 2009 17:55:51 martin odersky wrote:
>> NotNull is very much work in progress (or should I say: in
>> suspension?). We'll get back to it sometimes after 2.8 is released.
>>
>> Cheers
>>
>> -- Martin
>>
>>
>>
>> On Sat, Nov 21, 2009 at 2:50 PM, Erik Engbrecht
>>
>> wrote:
>>> That's different than what I'm suggesting. That would be:
>>> trait Option[+T <: NotNull]
>>> so that object contained by the Option can't be null. What I'm
>>> suggesting is:
>>> trait Option[+T] with NotNull
>>> so that the compiler will enforce that a reference to an Option is not
>>> null. The option would still be allowed to contain null.
>>> On Sat, Nov 21, 2009 at 8:42 AM, Kevin Wright
>>>
>>> wrote:
>>>> There's a lovely boundary condition when fetching i.e. the 5th item from
>>>> a list
>>>> None => The list doesn't have a 5th item
>>>> Some(xxx) => The 5th item exists and contains the value xxx
>>>> Some(null) => The 5th item exists and was explicitly populated with null
>>>>
>>>> On Sat, Nov 21, 2009 at 1:25 PM, Francois wrote:
>>>>> On 21/11/2009 13:53, Erik Engbrecht wrote:
>>>>>> I haven't dug into this much, so I could be completely off base, but
>>>>>> why doesn't Option extend NotNull?
>>>>> I believe that it was already discussed in (a, don't remember which)
>>>>> mailing list and that some corner cases make that modification rather
>>>>> disruptive for existing code. Or impossible.
>>>>>
>>>>> A mailing list archive digging may be helpful here.
>>>>>
>>>>>
>>>>> --
>>>>> Francois ARMAND
>>>>> http://fanf42.blogspot.com
>>> --
>>> http://erikengbrecht.blogspot.com/
>

David Flemström
Joined: 2009-08-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Option and NotNull

I would have liked to see it in 2.8. Who knows when 2.9 or even 3.0 will come
out? It will probably be a year or more until then.

On Sunday 22 November 2009 15:24:33 Jesper Nordenberg wrote:
> Martin just wrote it will be worked on after the 2.8 release, which is
> certainly more important than NotNull at this time. But I agree that it
> should be fixed in a not too distant future.
>
> /Jesper Nordenberg
>
> David Flemström wrote:
> > It's sad that this has such a low priority; about 50% of the bugs I get
> > when doing Java inter-operation come from NPE's, or the lack thereof as
> > the case may be.
> >
> > I would personally really have preferred if there wasn't a null in Scala
> > at all, and if Java methods would have thrown a NullException when they
> > return null (The null check for the exception throw would be optimized
> > away for the case when a special method "toOption(javaMethod())" (or
> > similarly named) was used)
> >
> > On Saturday 21 November 2009 17:55:51 martin odersky wrote:
> >> NotNull is very much work in progress (or should I say: in
> >> suspension?). We'll get back to it sometimes after 2.8 is released.
> >>
> >> Cheers
> >>
> >> -- Martin
> >>
> >>
> >>
> >> On Sat, Nov 21, 2009 at 2:50 PM, Erik Engbrecht
> >>
> >> wrote:
> >>> That's different than what I'm suggesting. That would be:
> >>> trait Option[+T <: NotNull]
> >>> so that object contained by the Option can't be null. What I'm
> >>> suggesting is:
> >>> trait Option[+T] with NotNull
> >>> so that the compiler will enforce that a reference to an Option is not
> >>> null. The option would still be allowed to contain null.
> >>> On Sat, Nov 21, 2009 at 8:42 AM, Kevin Wright
> >>>
> >>> wrote:
> >>>> There's a lovely boundary condition when fetching i.e. the 5th item
> >>>> from a list
> >>>> None => The list doesn't have a 5th item
> >>>> Some(xxx) => The 5th item exists and contains the value xxx
> >>>> Some(null) => The 5th item exists and was explicitly populated with
> >>>> null
> >>>>
> >>>> On Sat, Nov 21, 2009 at 1:25 PM, Francois wrote:
> >>>>> On 21/11/2009 13:53, Erik Engbrecht wrote:
> >>>>>> I haven't dug into this much, so I could be completely off base, but
> >>>>>> why doesn't Option extend NotNull?
> >>>>>
> >>>>> I believe that it was already discussed in (a, don't remember which)
> >>>>> mailing list and that some corner cases make that modification rather
> >>>>> disruptive for existing code. Or impossible.
> >>>>>
> >>>>> A mailing list archive digging may be helpful here.
> >>>>>
> >>>>>
> >>>>> --
> >>>>> Francois ARMAND
> >>>>> http://fanf42.blogspot.com
> >>>
> >>> --
> >>> http://erikengbrecht.blogspot.com/
>

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: Option and NotNull
But it can appear on a 2.8.1, for instance, which might not be as far as all that.

On Sun, Nov 22, 2009 at 1:09 PM, David Flemström <david [dot] flemstrom [at] gmail [dot] com> wrote:
I would have liked to see it in 2.8. Who knows when 2.9 or even 3.0 will come
out? It will probably be a year or more until then.

On Sunday 22 November 2009 15:24:33 Jesper Nordenberg wrote:
> Martin just wrote it will be worked on after the 2.8 release, which is
> certainly more important than NotNull at this time. But I agree that it
> should be fixed in a not too distant future.
>
> /Jesper Nordenberg
>
> David Flemström wrote:
> > It's sad that this has such a low priority; about 50% of the bugs I get
> > when doing Java inter-operation come from NPE's, or the lack thereof as
> > the case may be.
> >
> > I would personally really have preferred if there wasn't a null in Scala
> > at all, and if Java methods would have thrown a NullException when they
> > return null (The null check for the exception throw would be optimized
> > away for the case when a special method "toOption(javaMethod())" (or
> > similarly named) was used)
> >
> > On Saturday 21 November 2009 17:55:51 martin odersky wrote:
> >> NotNull is very much work in progress (or should I say: in
> >> suspension?). We'll get back to it sometimes after 2.8 is released.
> >>
> >> Cheers
> >>
> >>  -- Martin
> >>
> >>
> >>
> >> On Sat, Nov 21, 2009 at 2:50 PM, Erik Engbrecht
> >>
> >> <erik [dot] engbrecht [at] gmail [dot] com> wrote:
> >>> That's different than what I'm suggesting.  That would be:
> >>> trait Option[+T <: NotNull]
> >>> so that object contained by the Option can't be null.  What I'm
> >>> suggesting is:
> >>> trait Option[+T] with NotNull
> >>> so that the compiler will enforce that a reference to an Option is not
> >>> null. The option would still be allowed to contain null.
> >>> On Sat, Nov 21, 2009 at 8:42 AM, Kevin Wright
> >>>
> >>> <kev [dot] lee [dot] wright [at] googlemail [dot] com> wrote:
> >>>> There's a lovely boundary condition when fetching i.e. the 5th item
> >>>> from a list
> >>>> None => The list doesn't have a 5th item
> >>>> Some(xxx) => The 5th item exists and contains the value xxx
> >>>> Some(null) => The 5th item exists and was explicitly populated with
> >>>> null
> >>>>
> >>>> On Sat, Nov 21, 2009 at 1:25 PM, Francois <fanf42 [at] gmail [dot] com> wrote:
> >>>>> On 21/11/2009 13:53, Erik Engbrecht wrote:
> >>>>>> I haven't dug into this much, so I could be completely off base, but
> >>>>>> why doesn't Option extend NotNull?
> >>>>>
> >>>>> I believe that it was already discussed in (a, don't remember which)
> >>>>> mailing list and that some corner cases make that modification rather
> >>>>> disruptive for existing code. Or impossible.
> >>>>>
> >>>>> A mailing list archive digging may be helpful here.
> >>>>>
> >>>>>
> >>>>> --
> >>>>> Francois ARMAND
> >>>>> http://fanf42.blogspot.com
> >>>
> >>> --
> >>> http://erikengbrecht.blogspot.com/
>



--
Daniel C. Sobral

Veni, vidi, veterni.
Stepan Koltsov
Joined: 2008-12-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Option and NotNull

On Sun, Nov 22, 2009 at 18:24, Daniel Sobral wrote:
> But it can appear on a 2.8.1, for instance, which might not be as far as all
> that.

I hope, 2.8.x releases will contain bugfixes only.

S.

> On Sun, Nov 22, 2009 at 1:09 PM, David Flemström
> wrote:
>>
>> I would have liked to see it in 2.8. Who knows when 2.9 or even 3.0 will
>> come
>> out? It will probably be a year or more until then.
>>
>> On Sunday 22 November 2009 15:24:33 Jesper Nordenberg wrote:
>> > Martin just wrote it will be worked on after the 2.8 release, which is
>> > certainly more important than NotNull at this time. But I agree that it
>> > should be fixed in a not too distant future.
>> >
>> > /Jesper Nordenberg
>> >
>> > David Flemström wrote:
>> > > It's sad that this has such a low priority; about 50% of the bugs I
>> > > get
>> > > when doing Java inter-operation come from NPE's, or the lack thereof
>> > > as
>> > > the case may be.
>> > >
>> > > I would personally really have preferred if there wasn't a null in
>> > > Scala
>> > > at all, and if Java methods would have thrown a NullException when
>> > > they
>> > > return null (The null check for the exception throw would be optimized
>> > > away for the case when a special method "toOption(javaMethod())" (or
>> > > similarly named) was used)
>> > >
>> > > On Saturday 21 November 2009 17:55:51 martin odersky wrote:
>> > >> NotNull is very much work in progress (or should I say: in
>> > >> suspension?). We'll get back to it sometimes after 2.8 is released.
>> > >>
>> > >> Cheers
>> > >>
>> > >>  -- Martin
>> > >>
>> > >>
>> > >>
>> > >> On Sat, Nov 21, 2009 at 2:50 PM, Erik Engbrecht
>> > >>
>> > >> wrote:
>> > >>> That's different than what I'm suggesting.  That would be:
>> > >>> trait Option[+T <: NotNull]
>> > >>> so that object contained by the Option can't be null.  What I'm
>> > >>> suggesting is:
>> > >>> trait Option[+T] with NotNull
>> > >>> so that the compiler will enforce that a reference to an Option is
>> > >>> not
>> > >>> null. The option would still be allowed to contain null.
>> > >>> On Sat, Nov 21, 2009 at 8:42 AM, Kevin Wright
>> > >>>
>> > >>> wrote:
>> > >>>> There's a lovely boundary condition when fetching i.e. the 5th item
>> > >>>> from a list
>> > >>>> None => The list doesn't have a 5th item
>> > >>>> Some(xxx) => The 5th item exists and contains the value xxx
>> > >>>> Some(null) => The 5th item exists and was explicitly populated with
>> > >>>> null
>> > >>>>
>> > >>>> On Sat, Nov 21, 2009 at 1:25 PM, Francois wrote:
>> > >>>>> On 21/11/2009 13:53, Erik Engbrecht wrote:
>> > >>>>>> I haven't dug into this much, so I could be completely off base,
>> > >>>>>> but
>> > >>>>>> why doesn't Option extend NotNull?
>> > >>>>>
>> > >>>>> I believe that it was already discussed in (a, don't remember
>> > >>>>> which)
>> > >>>>> mailing list and that some corner cases make that modification
>> > >>>>> rather
>> > >>>>> disruptive for existing code. Or impossible.
>> > >>>>>
>> > >>>>> A mailing list archive digging may be helpful here.
>> > >>>>>
>> > >>>>>
>> > >>>>> --
>> > >>>>> Francois ARMAND
>> > >>>>> http://fanf42.blogspot.com
>> > >>>
>> > >>> --
>> > >>> http://erikengbrecht.blogspot.com/
>> >
>
>
>
> --
> Daniel C. Sobral
>
> Veni, vidi, veterni.
>

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