62

Kotlin: Type aliases and inline classes - ITNEXT

 4 years ago
source link: https://itnext.io/kotlin-type-aliases-and-inline-classes-ebe9d75f6715?gi=92487d3ea3eb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Kotlin: Type aliases and inline classes

Image for post
Image for post
Photo by Brendan Church on Unsplash

There is no doubt that Kotlin is here to stay for a long time, even more now that Google just announced in the last Google I/O 2019 that Kotlin is now Google’s preferred language for Android app development.

Today we’re announcing another big step: Android development will become increasingly Kotlin-first. Many new Jetpack APIs and features will be offered first in Kotlin. If you’re starting a new project, you should write it in Kotlin; code written in Kotlin often mean much less code for you–less code to type, test, and maintain.

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.

Inline Classes

Sometimes it is necessary for business logic or code readability to create a wrapper around some type but this creates a runtime overhead due to additional heap allocations.

In order to avoid this situation in Kotlin 1.3 Inline classes where introduced (experimental ⚠️feature). These special kind of classes make this improvement possible because the data is inlined into its usages, in fact the object instantiation is skipped.

Inline classes must have a single property initialized in the primary constructor.

declaration of an inline class

As mentioned before when you use an inline class no actual instantiation happens. At runtime, instances of the inline class will be represented using this single property. Look at the following example:

No actual instantiation of the classes Age & Height happens, at runtime ageUser1 and height1 contains just Int

Like regular classes, they can also have properties, functions and can inherit from interfaces, however there are some restrictions:

  • init blocks aren’t allowed;
  • properties cannot have backing fields;
  • cannot participate in a class hierarchy, this means that they are not allowed to extend other classes, they must be final.

If you got here you probably asking yourself:

Aren’t inline classes the same as type aliases?

I know the pain 😅

They seem to appear to be very similar since both seem to introduce a new type and both will be represented as the underlying type at runtime.

However they are different:

  • inline classes introduce a truly new type;
  • type aliases only introduce an alternative name (alias) for an existing type.

Since they are still experimental, Android Studio will display some warnings when you use of them. The warnings can be disabled by defining explicit compiler flags in your gradle file.

You can read more about type aliases and inline classes in the official documentation:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK