简体   繁体   中英

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

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM