October 2019
Intermediate to advanced
434 pages
11h 54m
English
Enums in Kotlin are very similar to those in Java. In the following we've defined a basic enum class named ArticleType:
enum class ArticleType { Aside, Blog, Series}
This then can be used as a restricted type anywhere we would use any other existing type. In the following snippet, we've defined a property on Article using the new ArticleType enum:
data class Article( var title: String, val author: String, val type: ArticleType)
When instantiating an instance of Article, we can reference the specific values exposed in our ArticleType enum:
fun main(args: Array<String>) { val article1 = Article( "Kotlin is great", "Nate", ArticleType.Blog )}
By limiting the article types to those defined within ArticleType, we've provided a clear contract ...
Read now
Unlock full access