Extension Properties and Computed Extensions
Add computed properties to existing classes without subclassing.
Extension Properties
Extension properties add computed properties to existing types without subclassing. They have no backing field.
Read-Only Extension Property
Define a getter to create a computed extension property.
val String.wordCount: Int
get() = trim().split(Regex("\\s+")).size
println("hello world kotlin".wordCount) // 3All lessons in this course
- Extension Functions: Syntax and Dispatch Rules
- Extension Properties and Computed Extensions
- Scoped Extensions and Companion Extensions
- Practical Extensions: Context, View & String Helpers