Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why productIterator return type Iterator[Any]?

Tags:

tuples

scala

Why scala Tuple productIterator return type Iterator[Any] ?

For example if Tuple3 or Product3 productIterator following definition

def productIterator[T1<:X,T2<:X,T3<:X,X] = Iterator(_1,_2,_3)

following Expression can return Iterator[java.lang.Number]

(BigInt(1),new java.lang.Long(2),new java.lang.Float(3)).productIterator

But current scala version (2.9.1) is not so . Is there a reason for something ?

like image 432
Kenji Yoshida Avatar asked Nov 05 '22 09:11

Kenji Yoshida


1 Answers

% scala3 -Xexperimental 
Welcome to Scala version 2.10.0.rdev-4056-2011-12-21-g69ed0a9 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class Foo[T1 <: java.lang.Number, T2 <: java.lang.Number](s: T1, t: T2)
defined class Foo

scala> Foo[java.lang.Integer, java.lang.Long](5, 5L)
res1: Foo[Integer,Long] = Foo(5,5)

scala> res1.productIterator _
res2: () => Iterator[Number] = <function0>

UPDATE

It is the least upper bound of the (unerased) types of the product elements. For instance in a Product[T, U, V] it's the same type as an expression

if (cond1) x1: T else if (cond2) x2: U else x3: V

or the type of the List in

List(x1: T, x2: U, x3: V)

In the repl look at the inferred type of f in

def f[T] = List(null: List[T], null: Set[T], null: Iterator[T])

to see what I mean.

like image 151
psp Avatar answered Nov 15 '22 07:11

psp