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

why isn't the correct type inferred?

6 replies
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.

(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

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: why isn't the correct type inferred?
On Sat, Jun 4, 2011 at 10:33 AM, HamsterofDeath <h-star@gmx.de> wrote:
(1,2,3,4,5,6).productIterator returns Iterator[Any]. why not Iterator[Int]?

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.  
in a pattern match, this works perfectly fine.
x match {
 case 1 => 1
 case 2 => 2
}

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
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
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
>
Alec Zorab
Joined: 2010-05-18,
User offline. Last seen 42 years 45 weeks ago.
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
>>
>

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
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
> >>
> >
Matthew Pocock 3
Joined: 2010-07-30,
User offline. Last seen 42 years 45 weeks ago.
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:
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
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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

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