Nulls (null)
A null is used to refer to the intentional absence of a value.
While it can be used as a separate type directly, null most commonly used to make an existing type explicitly nullable:
- TypeScript
- Swift
- Kotlin
- C++
import { type HybridObject, iOS } from 'nitro-core'
@iOS(...)
export declare class Math extends HybridObject {
a: number | null
}
class HybridMath: HybridMathSpec {
var a: Variant_NullType_Double
}
class HybridMath: HybridMathSpec() {
override var a: Variant_NullType_Double
}
class HybridMath: public HybridMathSpec {
std::variant<NullType, double> getA() override;
void setA(const std::variant<NullType, double>& value) override;
};
The NullType is a singleton structure in Nitro. To return null to JS:
- Swift
- Kotlin
- C++
func getNull() -> NullType {
return .null
}
fun getNull(): NullType {
return NullType.NULL
}
NullType getNull() {
return nitro::null;
}
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.
If you simply want to make an existing type optional, use Optionals instead.