Option[A] factory methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Option[A] factory methods

Chris Marshall
Is there any reason why we cannot add the Scalaz option factory methods to the core library on the Option object?

 var foo = none[Foo]
 var bar = some(aBar)

They just read much better than:

 val foo: Option[Foo] = none[Foo]
 val bar: Option[Bar] = some(aBar)

And the implementations are rather simple! Should I enter this in trac?

Chris



Get a new e-mail account with Hotmail - Free. Sign-up now.
Reply | Threaded
Open this post in threaded view
|

Re: Option[A] factory methods

Rex Kerr-2
var bar = Some(aBar)

already exists.  So I guess you want a convenience method for None?

var foo = None.of[Foo]

That would be nice.

  --Rex

P.S. In None:
  def of[A]: Option[A] = this

On Thu, Apr 8, 2010 at 7:28 AM, christopher marshall <[hidden email]> wrote:
Is there any reason why we cannot add the Scalaz option factory methods to the core library on the Option object?

 var foo = none[Foo]
 var bar = some(aBar)

They just read much better than:

 val foo: Option[Foo] = none[Foo]
 val bar: Option[Bar] = some(aBar)

And the implementations are rather simple! Should I enter this in trac?

Chris



Get a new e-mail account with Hotmail - Free. Sign-up now.

Reply | Threaded
Open this post in threaded view
|

Re: Option[A] factory methods

Martin Odersky
In reply to this post by Chris Marshall


On Thu, Apr 8, 2010 at 1:28 PM, christopher marshall <[hidden email]> wrote:
Is there any reason why we cannot add the Scalaz option factory methods to the core library on the Option object?

 var foo = none[Foo]
 var bar = some(aBar)

They just read much better than:

 val foo: Option[Foo] = none[Foo]
 val bar: Option[Bar] = some(aBar)

And the implementations are rather simple! Should I enter this in trac?

I think this would have a high chance of being closed as a won't fix. More methods are not automatically better. There needs to be some debate and broad consensus that we should indeed add this. According to my personal yardstick, the benefits would be rather marginal. And there would be immediately a question, why stop at Some/None. Should not _every_ case class have a lower case factory method that
returns the base type?

Cheers

 -- Martin


Reply | Threaded
Open this post in threaded view
|

RE: Option[A] factory methods

Chris Marshall
In reply to this post by Rex Kerr-2
No - both because the scala type inferencer will assume that the type of bar is Some[T], not Option[T] as follows:

scala> var bar = Some("A")
bar: Some[java.lang.String] = Some(A)

scala> bar = None
<console>:5: error: type mismatch;
 found   : None.type (with underlying type object None)
 required: Some[java.lang.String]
       bar = None
             ^
Hence I had to type the variable in my example below


Date: Thu, 8 Apr 2010 11:03:47 -0400
Subject: Re: [scala-debate] Option[A] factory methods
From: [hidden email]
To: [hidden email]

var bar = Some(aBar)

already exists.  So I guess you want a convenience method for None?

var foo = None.of[Foo]

That would be nice.

  --Rex

P.S. In None:
  def of[A]: Option[A] = this

On Thu, Apr 8, 2010 at 7:28 AM, christopher marshall <[hidden email]> wrote:
Is there any reason why we cannot add the Scalaz option factory methods to the core library on the Option object?

 var foo = none[Foo]
 var bar = some(aBar)

They just read much better than:

 val foo: Option[Foo] = none[Foo]
 val bar: Option[Bar] = some(aBar)

And the implementations are rather simple! Should I enter this in trac?

Chris



Get a new e-mail account with Hotmail - Free. Sign-up now.



Get a new e-mail account with Hotmail - Free. Sign-up now.
Reply | Threaded
Open this post in threaded view
|

Re: Option[A] factory methods

Colin Bullock
Most of this functionality is already available in 2.8.

scala> var bar = Option("A")
bar: Option[java.lang.String] = Some(A)

scala> bar = Option(null)
bar: Option[java.lang.String] = None

Granted, the null isn't particularly attractive[1] in this case, but I think the solution is much more generally useful.

- Colin

[1] Perhaps the parameter to Option.apply could be changed to have a default value of null (allowing for Option[String]()) but I'm not certain that adds a whole lot over a simple type ascription with None.
Reply | Threaded
Open this post in threaded view
|

RE: Option[A] factory methods

Razvan Cojocaru
In reply to this post by Chris Marshall
Would adding this to None work?

case object None extends Option[Nothing] {
  def apply[T] () : Option[T] = None
}

so you could do
bar = None[String]()
or even
bar = None[String]

- granted, no big gain...type system guesses very well most of the time...and the "pattern" of declaring the type when not certain may work better than this...

scala> case object NewNone  {
     |   def apply[T] () : Option[T] = None
     | }
defined module NewNone

scala> var bar = Option("a")
bar: Option[java.lang.String] = Some(a)

scala>     bar=NewNone[String]()
bar: Option[java.lang.String] = None

scala> bar=NewNone[String]
bar: Option[java.lang.String] = None

playground here: http://bit.ly/bd64Kx

christopher marshall-2 wrote
No - both because the scala type inferencer will assume that the type of bar is Some[T], not Option[T] as follows:

scala> var bar = Some("A")
bar: Some[java.lang.String] = Some(A)

scala> bar = None
<console>:5: error: type mismatch;
 found   : None.type (with underlying type object None)
 required: Some[java.lang.String]
       bar = None
             ^
Hence I had to type the variable in my example below

Date: Thu, 8 Apr 2010 11:03:47 -0400
Subject: Re: [scala-debate] Option[A] factory methods
From: ichoran@gmail.com
To: scala-debate@listes.epfl.ch

var bar = Some(aBar)

already exists.  So I guess you want a convenience method for None?

var foo = None.of[Foo]

That would be nice.

  --Rex

P.S. In None:
  def of[A]: Option[A] = this


On Thu, Apr 8, 2010 at 7:28 AM, christopher marshall <oxbow_lakes@hotmail.com> wrote:






Is there any reason why we cannot add the Scalaz option factory methods to the core library on the Option object?

 var foo = none[Foo]
 var bar = some(aBar)

They just read much better than:

 val foo: Option[Foo] = none[Foo]


 val bar: Option[Bar] = some(aBar)

And the implementations are rather simple! Should I enter this in trac?

Chris


     
Get a new e-mail account with Hotmail - Free. Sign-up now.

     
_________________________________________________________________
http://clk.atdmt.com/UKM/go/195013117/direct/01/
We want to hear all your funny, exciting and crazy Hotmail stories. Tell us now
Razvan Cojocaru,
Work: http://www.sigma-systems.com
Playground: http://wiki.homecloud.ca
Latest cool toy: Scripster
Follow me: RSS Feed, Twitter, GitHub.
Reply | Threaded
Open this post in threaded view
|

Re: Option[A] factory methods

Eric Willigers
In reply to this post by Chris Marshall

I think I'd like the opposite:-

List has
   def ::[B >: A] (x: B): List[B] =
     new scala.collection.immutable.::(x, this)

I'd like the return type to be ::


a :: b match {
   case x :: y => ...

   // the compiler incorrectly thinks we need to also consider case Nil
}