Kotlin is a modern programming language and so today we are going to see two cool features: type aliases and inline classes.

Type Aliases

Type aliases provide you a good way to set alternative names for existing types.

We all passed by the situation where a certain class a name too long or complex — MyAwesomeSupperDupperPowerClass . With type aliases you can introduce a different shorter or meaningful name and use that new one instead.

Let’s imagine the following scenario where you set a pair of strings with a username and password.

I’ve created an instance Pair that as an username and hashed password, however that doesn’t gives me much context and isn’t very clear.

Wouldn’t be better something like this:

This is gives you a better context and readability to your code.

Type aliases do not introduce new types. They are equivalent to the corresponding underlying types.

Another good example where you can use type aliases is when you need to use in the same place two classes with the same name but different packages. When this happens you typically referenced the second class by its fully-qualified class name. This situation can be bypassed this way:

Type aliases can also be used to name function types and also use them together with function type parameter names.