1

I would like a function to consume tuple of 7 but compiler won't let me with the shown message. I failed to find a proper way how to do it. Is it even possible without explicitely typing all the type parameters like Tuple7[String,String...,String] and is it even a good idea to use Scala like this ?

def store(record:Tuple7): Unit = {

  }

Error:(25, 20) class Tuple7 takes type parameters

def store(record: Tuple7): Unit = { ^

2
  • 6
    I don't think it's generally a good idea to use tuples of that size, especially if they store 7 items of the same type (String). You should probably consider using a case class for this, since you can explicitly name each item. Commented Dec 25, 2015 at 20:06
  • 1
    @dev-null compiler asks you to specify type of Tuple, e.g. ("foo", "bar") is Tuple2[String, String], you can workaround this with Tuple2[_, _] if you don't care about specific type, or use case class or make it less verbose with type alias: type S = String; Tuple3[S, S, S] Commented Dec 25, 2015 at 20:16

2 Answers 2

3

As stated by Luis you have to define what Type goes on which position for every position in the Tuple.

I`d like to add some approaches to express the same behaviour in different ways:

Tuple Syntax

For that you have two choices, what syntax to use to do so:

Tuple3[String, Int, Double]
(String, Int, Double)

Approach using Case Classes for better readability

Long tuples are hard to handle, especially when types are repeated. Scala offers a different approach for handling this. Instead of a Tuple7 you can use a case class with seven fields. The gain in this approach would be that you now can attach speaking names to each field and also the typing of each position makes more sense if a name is attached to it. And the chance of putting values in wrong positions is reduced

(String, Int, String, Int)
// vs
case class(name: String, age: Int, taxNumber: String, numberOfChildren: Int)

using Seq with pattern matching

If your intention was to have a sequence of data seq in combination with pattern matching could also be a nice fit:

List("name", 24, "", 5 ) match {
  case name:String :: age:Int ::_ :: _ :: Nil => doSomething(name, age)
}

This only works nice in a quite reduced scope. Normally you would lose a lot of type information as the List is of type Any.

Sign up to request clarification or add additional context in comments.

Comments

2

You could do the following :

def store(record: (String, String, String, String, String, String, String)):Unit = {
  }

which is the equivalent of :

def store(record: Tuple7[String, String, String, String, String, String, String]):Unit = {
  }

You can read more about it in Programming in Scala, 2nd Edition, chapter "Next Steps in Scala", sub-chapter "Step 9. use Tuples".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.