How to append or prepend an element to a tuple in Scala

EECOLOR picture EECOLOR · Mar 11, 2013 · Viewed 16.5k times · Source

I have a tuple and want to add an element without loosing type safety. This is what I want to achieve:

val tuple = ("", 1, 1f) // (String, Int, Float)

val newTuple:(String, Int, Float, Double) = tuple :+ 1d

Answer

Alex Archambault picture Alex Archambault · Jul 19, 2014

Shapeless now does it. Adding

import shapeless.syntax.std.tuple._

before your code just compiles.