- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Inferring type of None
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
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










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
RE: Inferring type of None
> Subject: Re: [scala-user] Inferring type of None
> From: contact [at] sciss [dot] de
> Date: Tue, 31 Jan 2012 18:19:31 +0000
> CC: scala-user [at] googlegroups [dot] com
> To: edmondo [dot] porcu [at] gmail [dot] com
>
> first of all, B <: Int doesn't make sense. Int is a final type, there cannot be any subtype of Int
>
Re: Inferring type of None
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 [at] hotmail [dot] com>
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]`.
Re: Re: Inferring type of None
Best
2012/2/1 Lars Hupel <hupel [at] in [dot] tum [dot] de>
RE: Re: Inferring type of None
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 [dot] porcu [at] gmail [dot] com
To: hupel [at] in [dot] tum [dot] de; scala-user [at] googlegroups [dot] com
Thank you very much, this is what I was looking for :)
Best
2012/2/1 Lars Hupel <hupel [at] in [dot] tum [dot] de>
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 [dot] porcu [at] gmail [dot] com
> To: hupel [at] in [dot] tum [dot] de; scala-user [at] googlegroups [dot] 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]`.
>
>
>
>
>
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 [at] sciss [dot] de
>> > Date: Tue, 31 Jan 2012 18:19:31 +0000
>> > CC: scala-user [at] googlegroups [dot] com
>> > To: edmondo [dot] porcu [at] gmail [dot] com
>>
>> >
>> > first of all, B <: Int doesn't make sense. Int is a final type, there
>> > cannot be any subtype of Int
>> >
>>
>
Re: Inferring type of None
for completeness: (None: Option[X])
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 [at] sciss [dot] de, scala-user [at] googlegroups [dot] 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 [at] sciss [dot] de
> >> > Date: Tue, 31 Jan 2012 18:19:31 +0000
> >> > CC: scala-user [at] googlegroups [dot] com
> >> > To: edmondo [dot] porcu [at] gmail [dot] com
> >>
> >> >
> >> > first of all, B <: Int doesn't make sense. Int is a final type, there
> >> > cannot be any subtype of Int
> >> >
> >>
> >