- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Inferring type of None
Tue, 2012-01-31, 19:12
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
Wed, 2012-02-01, 09:31
#2
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
>
> 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
>
Wed, 2012-02-01, 10:01
#3
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>
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
>
Wed, 2012-02-01, 10:21
#4
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
>> >
>>
>
Wed, 2012-02-01, 10:31
#5
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]`.
Wed, 2012-02-01, 10:41
#6
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>
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]`.
Wed, 2012-02-01, 11:51
#7
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
> >> >
> >>
> >
Wed, 2012-02-01, 12:31
#8
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>
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]`.
Fri, 2012-02-03, 04:31
#9
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]`.
>
>
>
>
>
Tue, 2012-02-07, 11:31
#10
Re: Inferring type of None
for completeness: (None: Option[X])
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