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?
Kotlin 1.1 supporting type aliases is now out!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With