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

Tuple Type / Type Projection

1 reply
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
scala> trait Test {
     |   type A <: {type X}
     |   type B = A#X
     |   type C = (A)#X
     | }
defined trait Test

What does "(A)#X" parse as in the above example? My reading of the language reference suggests it would expand to Tuple1[A]#X and consequently fail to compile, as below.

scala> trait Test1 {
     |   type A <: {type X}
     |   type B = Tuple1[A]#X
     | }
<console>:6: error: type X is not a member of (Test1.this.A,)
         type B = Tuple1[A]#X

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: Tuple Type / Type Projection
A better read of the spec reveals that: "A tuple type (T1 , . . . , Tn ) is an alias for the class scala.Tuplen[T1 , . . . , Tn ], where n ≥ 2"

So if n=1, the parens are just for grouping. Makes sense, I guess, my confusion is a consequence of the overloaded meaning of parentheses. Perhaps the spec could be updated to be explicit on this point?

-jason

scala> object A { type T = (Int, Int)}
defined module A

scala> var at: A.T = (0, 0)          
at: (Int, Int) = (0,0)

scala> object A { type T = (Int)}                                       
defined module A

scala> var at: A.T = 0          
at: A.T = 0

scala> object A { type T = ((Int))}                                     
defined module A

scala> var at: A.T = 0            
at: A.T = 0


On Tue, Jun 9, 2009 at 12:13 AM, Jason Zaugg <jzaugg@gmail.com> wrote:
scala> trait Test {
     |   type A <: {type X}
     |   type B = A#X
     |   type C = (A)#X
     | }
defined trait Test

What does "(A)#X" parse as in the above example? My reading of the language reference suggests it would expand to Tuple1[A]#X and consequently fail to compile, as below.

scala> trait Test1 {
     |   type A <: {type X}
     |   type B = Tuple1[A]#X
     | }
<console>:6: error: type X is not a member of (Test1.this.A,)
         type B = Tuple1[A]#X


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