is, as, Casting
is(type query)- See Cpp2 is:
obj is Int(i.e. a type)objPtr is T*instead ofdynamic_cast<T*>(objPtr) != NullPtrobj is cilia::Array(i.e. a template)obj is cilia::Integer(i.e. a concept)
- TODO Also support value query?
- See Cpp2 is:
as- See Cpp2 as
obj as Tinstead ofT(obj)objPtr as T*instead ofdynamic_cast<T*>(objPtr)- With
Variant vwhere T is one alternative:
v as Tinstead ofstd::get<T>(v) - With
Any a:
a as Tinstead ofstd::any_cast<T>(a) - With
Optional<T> o:
o as Tinstead ofo.value()
- See Cpp2 as
- Constructor casting
Float(3)- Casting via constructor is
explicitby default,implicitas option. - No classic C-style casting:
(Float) 3 - but also
castToMutable<T>(...)ormutableCastTo<T>(...)- instead of
constCastTo<>(...)
- instead of
reinterpretCastTo<T>(...)staticCastTo<T>(...)?
- Automatic casts
- as in Kotlin,
- for template types, references and pointers.
-
func getStringLength(Type obj) -> Int { if obj is String { // "obj" is automatically cast to "String" in this branch return obj.length } // "obj" is still a "Type" outside of the type-checked branch return 0 } -
func getStringLength(Type obj) -> Int { if not obj is String return 0 // "obj" is automatically cast to "String" in this branch return obj.length } -
func getStringLength(Type obj) -> Int { // "obj" is automatically cast to "String" on the right-hand side of "and" if obj is String and obj.length > 0 { return obj.length } return 0 } - TODO Multiple inheritance is problematic here:
- In Cilia/C++, an object can be an instance of several base classes at once, whereby the pointer (sometimes) changes during casting.
- What if you still want/need to access the functions for a
Type objafterif obj is ParentA?- Workaround:
- In case of multiple inheritance there is no automatic casting.
Cast back withType(obj).functionOfA()
- Workaround: