Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin `typealias` feature

According to the source of Kotlin's lexer, there is a typealias keyword, and it's not "reserved for future use" like yield and typeof.

Also, the grammar reference suggests that typealias should be a valid keyword for a class member declaration, and when I type typealias in IntelliJ IDEA (Android Studio) with Kotlin plugin it recognizes it as a keyword but I get expecting member declaration error. I have also tried using it with the "usual" syntax, for example like it is implemented in Swift, however with no success.

So, is the typealias feature actually implemented in Kotlin (as of 1.0), and if so, what is the syntax for it? Is there any documentation that describes its use?


Update

Kotlin 1.1 supporting type aliases is now out!

like image 320
maciekjanusz Avatar asked Feb 22 '16 12:02

maciekjanusz


People also ask

What is a Typealias in Kotlin?

Kotlin Vocabulary: typealias When type definitions distract from what your code means because they're not readable, expressive or just too long, Kotlin has just the right feature for you: typealias ! Typealias allows you to provide alternate names for class or functions types without introducing a new type.

What is Typealias?

A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program. Type alias do not create new types. They simply provide a new name to an existing type.

What are Typealias in Java?

Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead. It's useful to shorten long generic types. For instance, it's often tempting to shrink collection types: typealias NodeSet = Set<Network.

What is a unit Kotlin?

Unit: Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.


1 Answers

With a typealias, you can provide an alternative name for an existing type since Kotlin 1.1:

typealias Multimap<K, V> = MutableMap<K, MutableList<V>>

For more information, see the official documentation or the KEEP proposal.

like image 144
Alexander Udalov Avatar answered Sep 20 '22 08:09

Alexander Udalov