Type Aliasing
When working with a large library you may come across a class name that simply does not work for you. The name is either long or unwieldy, or you simply feel there is a better name that captures the abstraction. You have the freedom to alias, and give the class the name that will make you feel good.
Here’s a class with a rather long name:
WorkingWithObjects/PoliceOfficer.scala | |
| class PoliceOfficer(val name: String) |
Cop says it all and is easier to type as well. Here’s how we can alias the PoliceOfficer class without losing its identity:
WorkingWithObjects/CopApp.scala | |
| object CopApp extends App { |
| type Cop = PoliceOfficer |
| |
| val topCop = new Cop("Jack") |
| println(topCop.getClass) |
| } |
Compile and run the code to ...
Get Pragmatic Scala now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.