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

Inferring type of None

10 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Dear all,
I have the following class:
class MyClass[A,B<:Int](val firstValue:Option[A], val secondValue:Option[B])
When I pass None as one of the two parameters, the inferer cannot infer the type (which in fact does not matter)
How can one solve this elegantly?
Best Regards
Edmondo
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: Inferring type of None

first of all, B <: Int doesn't make sense. Int is a final type, there cannot be any subtype of Int

regarding the question. i don't understand the problem -- it works for me:

new MyClass( None, Some(33))

--> MyClass[Nothing, Int]

val y = new MyClass( Some( "hallo" ), None )

--> MyClass[String, Nothing]

what type would you prefer to have inferred? You know, that B <: Int indeed has one other possible solution than Int, it's Nothing which is a subtype of any other type.

best, -sciss-

On 31 Jan 2012, at 18:11, Edmondo Porcu wrote:

> Dear all,
>
> I have the following class:
>
> class MyClass[A,B<:Int](val firstValue:Option[A], val secondValue:Option[B])
>
> When I pass None as one of the two parameters, the inferer cannot infer the type (which in fact does not matter)
>
> How can one solve this elegantly?
>
> Best Regards
>
> Edmondo

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Inferring type of None
I think you'll find that Nothing is a subtype of Int

> Subject: Re: [scala-user] Inferring type of None
> From: contact@sciss.de
> Date: Tue, 31 Jan 2012 18:19:31 +0000
> CC: scala-user@googlegroups.com
> To: edmondo.porcu@gmail.com
>
> first of all, B <: Int doesn't make sense. Int is a final type, there cannot be any subtype of Int
>

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Inferring type of None
Yes sorry I oversimplified the example.
The solution I found to let the type inferer work is the following:
None.asInstanceOf[Option[MyType]]
Best Regards

2012/2/1 Chris Marshall <oxbow_lakes@hotmail.com>
I think you'll find that Nothing is a subtype of Int

> Subject: Re: [scala-user] Inferring type of None
> From: contact@sciss.de
> Date: Tue, 31 Jan 2012 18:19:31 +0000
> CC: scala-user@googlegroups.com
> To: edmondo.porcu@gmail.com
>
> first of all, B <: Int doesn't make sense. Int is a final type, there cannot be any subtype of Int
>


Alec Zorab
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Inferring type of None

On 1 February 2012 08:56, Edmondo Porcu wrote:
> Yes sorry I oversimplified the example.
>
> The solution I found to let the type inferer work is the following:
>
> None.asInstanceOf[Option[MyType]]
>
> Best Regards
>
>
> 2012/2/1 Chris Marshall
>>
>> I think you'll find that Nothing is a subtype of Int
>>
>> > Subject: Re: [scala-user] Inferring type of None
>> > From: contact@sciss.de
>> > Date: Tue, 31 Jan 2012 18:19:31 +0000
>> > CC: scala-user@googlegroups.com
>> > To: edmondo.porcu@gmail.com
>>
>> >
>> > first of all, B <: Int doesn't make sense. Int is a final type, there
>> > cannot be any subtype of Int
>> >
>>
>

Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: Inferring type of None

Can we please stop using `asInstanceOf` to "let the type inferencer
work"? That's nowhere near a "solution" to your problem.

If you need a `None` which is of type `Option[X]`, use `Option.empty[X]`.

edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 3 days ago.
Re: Re: Inferring type of None
Thank you very much, this is what I was looking for :)
Best

2012/2/1 Lars Hupel <hupel@in.tum.de>
Can we please stop using `asInstanceOf` to "let the type inferencer
work"? That's nowhere near a "solution" to your problem.

If you need a `None` which is of type `Option[X]`, use `Option.empty[X]`.


H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Inferring type of None

there is a general solution for this kind of problem that does not rely on special factory methods.

syntax details, top of page 3.
http://rapidshare.com/files/3045603786/scc.pdf

val myTypedNone = None:Option[Int]
has the same effect as the cast, but is safe and can't go wrong.

-------- Original-Nachricht --------
> Datum: Wed, 1 Feb 2012 09:15:27 +0000
> Von: Alec Zorab
> An: Edmondo Porcu
> CC: Chris Marshall , contact@sciss.de, scala-user@googlegroups.com
> Betreff: Re: [scala-user] Inferring type of None

> Alternatively: Option.empty[MyType]
>
> On 1 February 2012 08:56, Edmondo Porcu wrote:
> > Yes sorry I oversimplified the example.
> >
> > The solution I found to let the type inferer work is the following:
> >
> > None.asInstanceOf[Option[MyType]]
> >
> > Best Regards
> >
> >
> > 2012/2/1 Chris Marshall
> >>
> >> I think you'll find that Nothing is a subtype of Int
> >>
> >> > Subject: Re: [scala-user] Inferring type of None
> >> > From: contact@sciss.de
> >> > Date: Tue, 31 Jan 2012 18:19:31 +0000
> >> > CC: scala-user@googlegroups.com
> >> > To: edmondo.porcu@gmail.com
> >>
> >> >
> >> > first of all, B <: Int doesn't make sense. Int is a final type, there
> >> > cannot be any subtype of Int
> >> >
> >>
> >

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
RE: Re: Inferring type of None
Using scalaz:
    none[X], some[X](x), nil[X] etc
Scala should have these type constructors as part of the standard library IMHO

Date: Wed, 1 Feb 2012 10:21:14 +0100
Subject: Re: [scala-user] Re: Inferring type of None
From: edmondo.porcu@gmail.com
To: hupel@in.tum.de; scala-user@googlegroups.com

Thank you very much, this is what I was looking for :)
Best

2012/2/1 Lars Hupel <hupel@in.tum.de>
Can we please stop using `asInstanceOf` to "let the type inferencer
work"? That's nowhere near a "solution" to your problem.

If you need a `None` which is of type `Option[X]`, use `Option.empty[X]`.


Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Inferring type of None

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

FYI, x.some allows you to dispense of the type annotation.

scala> val r = 7.some
r: Option[Int] = Some(7)

On 02/01/2012 09:27 PM, Chris Marshall wrote:
>
> Using scalaz:
> none[X], some[X](x), nil[X] etc
> Scala should have these type constructors as part of the standard library IMHO
>
> Date: Wed, 1 Feb 2012 10:21:14 +0100
> Subject: Re: [scala-user] Re: Inferring type of None
> From: edmondo.porcu@gmail.com
> To: hupel@in.tum.de; scala-user@googlegroups.com
>
> Thank you very much, this is what I was looking for :)
> Best
>
> 2012/2/1 Lars Hupel
>
> Can we please stop using `asInstanceOf` to "let the type inferencer
>
> work"? That's nowhere near a "solution" to your problem.
>
>
>
> If you need a `None` which is of type `Option[X]`, use `Option.empty[X]`.
>
>
>
>
>

moors
Joined: 2010-10-06,
User offline. Last seen 36 weeks 4 days ago.
Re: Inferring type of None

for completeness: (None: Option[X])

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