Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpacking tuple types in Scala

I was just wondering, can I decompose a tuple type into its components' types in Scala?

I mean, something like this

trait Container {
  type Element
}

trait AssociativeContainer extends Container {
  type Element <: (Unit, Unit)
  def get(x : Element#First) : Element#Second
}
like image 480
jpalecek Avatar asked Feb 23 '09 00:02

jpalecek


People also ask

What is unpacking of tuple?

Unpacking a tuple Unpacking a tuple means splitting the tuple's elements into individual variables. For example: x, y = (1, 2) Code language: Python (python)

Can we have variables of different types inside of a tuple Scala?

Thankfully, Scala already has a built-in tuple type, which is an immutable data structure that we can use for holding up to 22 elements with different types.

Is tuple immutable in Scala?

In Scala, a tuple is a value that contains a fixed number of elements, each with its own type. Tuples are immutable. Tuples are especially handy for returning multiple values from a method.

What is packing and unpacking of tuple give example?

Tuple packing refers to assigning multiple values into a tuple. Tuple unpacking refers to assigning a tuple into multiple variables. You have already used tuple packing because it is as simple as: >>> django_movie = ("Tarantino", 2012, 8.4, "Waltz & DiCaprio")


1 Answers

You can't unpack, per se, but maybe this achieves what you want:

  type First
  type Second
  type Element = (First, Second)
  def get(x: First): Second
like image 116
Jorge Ortiz Avatar answered Dec 03 '22 04:12

Jorge Ortiz