How do you declare a type alias in a scala constructor?

Matthew Pickering picture Matthew Pickering · Mar 21, 2012 · Viewed 10k times · Source

If I have a class that takes a tuple in its constructor among other values such as:

class Foo(a: Int, b: String, c: (Int, String)) 

How do I use an abstract type to give the tuple a more descriptive name in a lightweight fashion (without wrapping it in a new class):

class Foo(a: Int, b: String, c: Dave) 

I'm not sure how to bring a type alias in scope (or if this is the best thing to do):

type Dave = (Int, String) 

Is there a convention for where to define types in this way (or should I be defining case classes and wrapping everything...)?

I appreciate that it does not make sense in a lot of situations but if I'm really only looking for a more descriptive name is it possible?

Thanks!

Answer

jxstanford picture jxstanford · Mar 21, 2012

You could use a type alias like:

scala> type MyTuple = Tuple2[Int,String]
defined type alias MyTuple

scala> val x = new MyTuple(1, "one")
x: (Int, String) = (1,one)