Declaration of repeated items in tuple

Hi all,

I’m studying scala Language Specificaton, version 2.11 .

I’m trying to declare a tuple using the “Repeated parameter” as wrotten in chapter 4.6.3, of SLS:

Repeated parameters

I tried on my own to declare a tuple in this way, but I can’t figure out, where I’m wrong, because it don’t works !

In the chapter above, it says:

“That is, if a method m with type (p1:T1,…,pn:Tn,ps:S *)U `` is applied to arguments (e1,…,ek) where k≥n, then m is taken in that application to have type (p1:T1,…,pn:Tn,ps:S,…,ps′S)U, with k−n occurrences of type S where any parameter names beyond ps are fresh”

So I’m trying to type:

val Tuple (x1 : Int ..., x5 : String ..., x8 : Float) = (e1... , e10);

And, it don’t works.
Which is the correct syntax to declare this kind of tuple?

Thank you

val tuple: (Int, ..., String) = (3, ..., "foo")

If you are new to the language I would recommend you to take a look to a tutorial rather than the spec. The spec is designed for the compiler contributors and tooling creators, not for newcomers.

1 Like

Thank you, but it seems to not work properly.

Seriously: that’s not how tuples are declared. As @BalmungSan says, the spec is focused on how the compiler authors think, so the ... just means “and then there might be more of these here”. It’s not something you can literally say, just spec shorthand.

Putting it more strongly: the spec is an awful way to learn the language, one of the worst ways I can possibly imagine. You really should just ignore it for now. In 12 years of working in Scala, I have referred to the spec maybe 10 times – it’s generally only useful when you know the language extremely well, and something is ambiguous or confusing.

If you want to know how the language’s author intended to teach it, I recommend Programming in Scala, which exists for that purpose and isn’t bad. The book I’m mostly using for my students nowadays is Essential Scala, which is relatively concise and clear.


And to the specific point: you can’t do that. The “Repeated Parameters” section is talking about how to declare parameter lists for functions, not tuples – if you declare the last parameter to a function as String*, that means that the caller can pass in any number of Strings there, and they are received in the function as a Seq[String]. You simply can’t do anything like that with Tuples: a Tuple always has a fixed length, pretty much by definition.

(Yes, the description is confusing – that’s part of why I recommend ignoring the spec.)