- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
why isn't the correct type inferred?
Sat, 2011-06-04, 18:33
(1,2,3,4,5,6).productIterator returns Iterator[Any]. why not Iterator[Int]?
in a pattern match, this works perfectly fine.
x match {
case 1 => 1
case 2 => 2
}
-> returns an Int
Mon, 2011-06-06, 17:47
#2
RE: why isn't the correct type inferred?
Because the method is declared on Tuple6[A, B, C, D, E, F] - the returned iterator must have a common supertype for all of A-F;
> Date: Sat, 4 Jun 2011 19:33:40 +0200
> From: h-star@gmx.de
> To: scala-user@googlegroups.com
> Subject: [scala-user] why isn't the correct type inferred?
>
> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not Iterator[Int]?
>
> in a pattern match, this works perfectly fine.
> x match {
> case 1 => 1
> case 2 => 2
> }
>
> -> returns an Int
>
> Date: Sat, 4 Jun 2011 19:33:40 +0200
> From: h-star@gmx.de
> To: scala-user@googlegroups.com
> Subject: [scala-user] why isn't the correct type inferred?
>
> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not Iterator[Int]?
>
> in a pattern match, this works perfectly fine.
> x match {
> case 1 => 1
> case 2 => 2
> }
>
> -> returns an Int
>
Mon, 2011-06-06, 18:07
#3
Re: why isn't the correct type inferred?
Sure, but in the case of Tuple6[Int, Int, Int, Int, Int, Int] there's
a pretty obvious common type that's more specific than Any.
That said, the most direct answer to the question is, as Alex
observed, because productIterator is defined as returning
Iterator[Any].
On Mon, Jun 6, 2011 at 5:36 PM, Chris Marshall wrote:
> Because the method is declared on Tuple6[A, B, C, D, E, F] - the returned
> iterator must have a common supertype for all of A-F;
>
>> Date: Sat, 4 Jun 2011 19:33:40 +0200
>> From: h-star@gmx.de
>> To: scala-user@googlegroups.com
>> Subject: [scala-user] why isn't the correct type inferred?
>>
>> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not
>> Iterator[Int]?
>>
>> in a pattern match, this works perfectly fine.
>> x match {
>> case 1 => 1
>> case 2 => 2
>> }
>>
>> -> returns an Int
>>
>
Mon, 2011-06-06, 18:57
#4
RE: why isn't the correct type inferred?
There is no way to encode this in Scala:
class CleverTuple[A, B, C, D, E, F](a : A, b : B, c : C, d : D, e : E, f : F) {
//no way of doing this! def itr[Z such that Z >: A, Z >: B, Z >: C, Z >: D, Z >: E, Z >: F] : Iterator[Z] = Iterator(a,b ,c ,d ,e, f)
}
> Date: Mon, 6 Jun 2011 17:49:38 +0100
> Subject: Re: [scala-user] why isn't the correct type inferred?
> From: aleczorab@googlemail.com
> To: oxbow_lakes@hotmail.com
> CC: scala-user@googlegroups.com
>
> Sure, but in the case of Tuple6[Int, Int, Int, Int, Int, Int] there's
> a pretty obvious common type that's more specific than Any.
>
> That said, the most direct answer to the question is, as Alex
> observed, because productIterator is defined as returning
> Iterator[Any].
>
> On Mon, Jun 6, 2011 at 5:36 PM, Chris Marshall <oxbow_lakes@hotmail.com> wrote:
> > Because the method is declared on Tuple6[A, B, C, D, E, F] - the returned
> > iterator must have a common supertype for all of A-F;
> >
> >> Date: Sat, 4 Jun 2011 19:33:40 +0200
> >> From: h-star@gmx.de
> >> To: scala-user@googlegroups.com
> >> Subject: [scala-user] why isn't the correct type inferred?
> >>
> >> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not
> >> Iterator[Int]?
> >>
> >> in a pattern match, this works perfectly fine.
> >> x match {
> >> case 1 => 1
> >> case 2 => 2
> >> }
> >>
> >> -> returns an Int
> >>
> >
class CleverTuple[A, B, C, D, E, F](a : A, b : B, c : C, d : D, e : E, f : F) {
//no way of doing this! def itr[Z such that Z >: A, Z >: B, Z >: C, Z >: D, Z >: E, Z >: F] : Iterator[Z] = Iterator(a,b ,c ,d ,e, f)
}
> Date: Mon, 6 Jun 2011 17:49:38 +0100
> Subject: Re: [scala-user] why isn't the correct type inferred?
> From: aleczorab@googlemail.com
> To: oxbow_lakes@hotmail.com
> CC: scala-user@googlegroups.com
>
> Sure, but in the case of Tuple6[Int, Int, Int, Int, Int, Int] there's
> a pretty obvious common type that's more specific than Any.
>
> That said, the most direct answer to the question is, as Alex
> observed, because productIterator is defined as returning
> Iterator[Any].
>
> On Mon, Jun 6, 2011 at 5:36 PM, Chris Marshall <oxbow_lakes@hotmail.com> wrote:
> > Because the method is declared on Tuple6[A, B, C, D, E, F] - the returned
> > iterator must have a common supertype for all of A-F;
> >
> >> Date: Sat, 4 Jun 2011 19:33:40 +0200
> >> From: h-star@gmx.de
> >> To: scala-user@googlegroups.com
> >> Subject: [scala-user] why isn't the correct type inferred?
> >>
> >> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not
> >> Iterator[Int]?
> >>
> >> in a pattern match, this works perfectly fine.
> >> x match {
> >> case 1 => 1
> >> case 2 => 2
> >> }
> >>
> >> -> returns an Int
> >>
> >
Mon, 2011-06-06, 19:07
#5
Re: why isn't the correct type inferred?
I'm guessing the alternative is for productIterator to be defined as:
def productIterator[A, B, C, D, E, F, Z < A < B < C < D < E < F](t6: Tuple6[A, B, C, D, E, F]): Iterator[Z]
This will nearly always be something like Any, but in this case you'd perhaps find yourself getting back an Iterator[Int]. Seems like a lot of typing (pun intended) for what I'm guessing is an unusual occurrence.
Matthew
On 6 June 2011 17:49, Alec Zorab <aleczorab@googlemail.com> wrote:
--
Matthew Pocockmailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com msn: matthew_pocock@yahoo.co.ukirc.freenode.net: drdozer(0191) 2566550
def productIterator[A, B, C, D, E, F, Z < A < B < C < D < E < F](t6: Tuple6[A, B, C, D, E, F]): Iterator[Z]
This will nearly always be something like Any, but in this case you'd perhaps find yourself getting back an Iterator[Int]. Seems like a lot of typing (pun intended) for what I'm guessing is an unusual occurrence.
Matthew
On 6 June 2011 17:49, Alec Zorab <aleczorab@googlemail.com> wrote:
Sure, but in the case of Tuple6[Int, Int, Int, Int, Int, Int] there's
a pretty obvious common type that's more specific than Any.
That said, the most direct answer to the question is, as Alex
observed, because productIterator is defined as returning
Iterator[Any].
On Mon, Jun 6, 2011 at 5:36 PM, Chris Marshall <oxbow_lakes@hotmail.com> wrote:
> Because the method is declared on Tuple6[A, B, C, D, E, F] - the returned
> iterator must have a common supertype for all of A-F;
>
>> Date: Sat, 4 Jun 2011 19:33:40 +0200
>> From: h-star@gmx.de
>> To: scala-user@googlegroups.com
>> Subject: [scala-user] why isn't the correct type inferred?
>>
>> (1,2,3,4,5,6).productIterator returns Iterator[Any]. why not
>> Iterator[Int]?
>>
>> in a pattern match, this works perfectly fine.
>> x match {
>> case 1 => 1
>> case 2 => 2
>> }
>>
>> -> returns an Int
>>
>
--
Matthew Pocockmailto: turingatemyhamster@gmail.comgchat: turingatemyhamster@gmail.com msn: matthew_pocock@yahoo.co.ukirc.freenode.net: drdozer(0191) 2566550
Mon, 2011-06-06, 19:17
#6
Re: why isn't the correct type inferred?
On Mon, Jun 6, 2011 at 7:54 PM, Matthew Pocock
wrote:
> I'm guessing the alternative is for productIterator to be defined as:
> def productIterator[A, B, C, D, E, F, Z < A < B < C < D < E < F](t6:
> Tuple6[A, B, C, D, E, F]): Iterator[Z]
> This will nearly always be something like Any, but in this case you'd
> perhaps find yourself getting back an Iterator[Int]. Seems like a lot of
> typing (pun intended) for what I'm guessing is an unusual occurrence.
I took an approach similar to this in Scalaz:
scala> case class T2[+A, +B](_1: A, _2: B) {
| def seq[Z](implicit ev: this.type <:< T2[Z, Z]): Seq[Z] = {
| val a = ev(this); Seq[Z](a._1, a._2)
| }
| }
defined class T2
scala> val t = T2(0, false)
t: T2[Int,Boolean] = T2(0,false)
scala> t.seq
res10: Seq[AnyVal] = List(0, false)
-jason
If there were some way to write its type signature as LUB(X1, ..., Xn) it could. But AFAIK there isn't. And even if there were, it would mean that the callers wouldn't be able to treat it uniformly.
productIterator is defined in the library (on the Product trait). Calculating the LUB of a branching expression, on the other hand, is a language feature.
-0xe1a