Feather2 / documentation / as.md
is, as, as?
is
exression is TYPE
Returns true iff expression is assignable to type TYPE.
e.g.
"Hello" is String // true
"Hello" is CharSequence // true, because String implements CharSequence
"Hello" is File // false
Similar to Java's instanceof operator and Kotlin's is operator.
Unlike Java's instanceof, TYPE can be a primitive type,
which is replaced by the boxed version. i.e. these are identical :
expression is int
expression is Integer
Feather scripts should never need to use the class names Integer, Double etc.
and the standard configuration doesn't import Integer, Double etc.
So the second example would fail, unless Integer was explicitly imported.
as
expression as TYPE
Returns the expression cast to TYPE, or throws a cast exception.
Equivalent to Java's (ugly?) casting syntax :
(TYPE) expression
Note, if expression is an object (boxed) type, no auto-casting is performed. e.g.
listOf<Object>( 42 )[0] as long // Fails, because we are attempting to cast an Integer to a Long.
Consider using : as Number instead. e.g.
val firstNumber : long = listOf<Object>( 42 )[0] as Number
as?
expression as? TYPE
Similar to as, but if expression is not the correct type, then
null is returned, and no exception is thrown.
However, unlike as no conversion between primitive types are performed.
e.g.
listOf<Object>( 42 ) as? double // returns null, because it is an Integer, not a Double
listOf<Object>( 42 ) as double // returns 42.0
The result is always an object (or null), never a primitive.