Optionals (T?)
Optional or undefined values can be declared either by using the questionmark operator (?), or by adding an undefined variant:
- TypeScript
- Swift
- Kotlin
- C++
interface Math extends HybridObject<{ … }> {
a?: number
b: number | undefined
}
class HybridMath: HybridMathSpec {
var a: Double?
var b: Double?
}
class HybridMath: HybridMathSpec() {
override var a: Double?
override var b: Double?
}
class HybridMath: public HybridMathSpec {
std::optional<double> a;
std::optional<double> b;
};
In Kotlin/Java, nullables have to be boxed in object types.
Optionals vs null
In the same way that JavaScript distinguishes between an optional type/undefined and null, Nitro also has two separate concepts for the two.
An optional type (or undefined) represents a non-declared value, whereas null represents an explicit absence of a value. See Nulls for more information.