Feather2 / documentation / ExtensionFunctions.md
Extension Functions
Feather supports extension functions, similar to Kotlin, with one difference. Any static function can act as an extension function. They can be imported from pre-existing Java/Kotlin libraries, or written in Feather.
Example of an extension function written in Feather :
class MyExtensions {
static fun first( str : String ) = str[0]
}
We can now use this extension function like so :
import static MyExtensions.*
class Person {
var name = "Nick"
fun initial() = name.first()
}
}
It can also be written like this : // TODO At the time of writing, Feather doesn't support this.
class MyExtensions {
static fun String.first() = this[0]
}
The difference is merely cosmetic, pick whichever you think is appropriate.
Note, the usual access rules apply, so protected
or private
methods of
the extended class cannot be used within the extension function's body.
Unlike Kotlin, Feather does not support functions at the top-level.
All functions must be declared within a class
.
It is good practice to make the constructor private
, so that no instances
of the class can be created.
// TODO Feather doesn't support private constructors at the time of writing!
Built-In Extension Functions
The feather-runtime
has three classes packed with extension functions :
FeatherCollections
: for List, Map and arraysFileExtensions
: for FileFeatherExtensions
: A jumble of additional extensions.
The default configuration allow import java.lang.Arrays
,
whose methods can be thought of as extension functions for array types.