17

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!

Matthew Pickering
  • 523
  • 1
  • 5
  • 16

1 Answers1

10

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)
jxstanford
  • 3,339
  • 3
  • 27
  • 39