The shortest I could come up with:
scala> "hello_world" split("_") match { case Array(f, s) => (f, s) }
res0: (String, String) = (hello,world)
Of course it will throw for other non Tuple2
cases. You can have this to avoid exceptons:
scala> "hello_world" split("_") match { case Array(f, s) => Some(f, s); case _ => None }
res1: Option[(String, String)] = Some((hello,world))
Or with implicits:
scala> implicit class ArrayTupple[T](val a: Array[T]) { def pair = a match { case Array(f, s) => (f, s) } }
defined class ArrayTupple
scala> val ss = "hello_world".split("_")
ss: Array[String] = Array(hello, world)
scala> ss.pair
res0: (String, String) = (hello,world)
I'm still not very happy with the solution. Seems like you have to either write out all possible cases up to Tuple22
to make more generic method like to[Tuple22]
or maybe use macros.