Class "aliasing" with "as" - so cool, like C#


#1

More of a “this is amazing” than a question.

In our previous codebases for REST APIs, we have a design / pattern of establishing models/dtos for the API interface, and another set (which may or may not be identical) for the service/repository/data layer.

One annoying thing in Java was that we may have two models called “Coffee”, and so we then had to work around it by doing CoffeeModel and Coffee, or CoffeeDTO and CoffeeModel or various combinations. The reasons for this is in our mappers we would have to say coffee.name = coffeeDto.name (using a mapping library for most).

With Kotlin, we can now just have “import my.package.Coffee as ApiModel” and “import my.package.dto.Coffee as DtoModel”.

Brilliant!