Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created March 19, 2015 23:14
Show Gist options
  • Save fancellu/2395b8e614c48d096990 to your computer and use it in GitHub Desktop.
Save fancellu/2395b8e614c48d096990 to your computer and use it in GitHub Desktop.
Case to tuple and back
case class Person(name:String,age:Int)
val dino=Person("dino",48) //> dino : stuff.Person = Person(dino,48)
// get tuple from case class, decompose
val (name,age)=Person.unapply(dino).get //> name : String = dino
//| age : Int = 48
// create case class from composed tuple
val dino2=Person.tupled((name,age)) //> dino2 : stuff.Person = Person(dino,48)
dino==dino2 //> res10: Boolean = true
@fancellu
Copy link
Author

fancellu commented Sep 2, 2016

http://michalostruszka.pl/blog/2015/03/30/scala-case-classes-to-and-from-tuples/

Note complication with tupled if case class has a companion object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment