Primitives (number, boolean, bigint)
Primitive datatypes like number, boolean or bigint can use platform-native datatypes directly.
For example, a JS number is always a 64-bit double in C++, a Double in Swift, and a Double in Kotlin.
- TypeScript
- Swift
- Kotlin
- C++
interface Math extends HybridObject<{ … }> {
add(a: number, b: number): number
}
class HybridMath: HybridMathSpec {
func add(a: Double, b: Double) -> Double
}
class HybridMath: HybridMathSpec() {
fun add(a: Double, b: Double): Double
}
class HybridMath : public HybridMathSpec {
double add(double a, double b);
}
Primitives are very efficient and can be passed with little to no overhead, especially between C++ and Swift, and C++ and Kotlin.
bigint
While a bigint is technically a primitive in JS, it is not fully representable by any standard library type in C++, Kotlin or Swift.
In Nitro, you can use bigint as either a signed, or unsigned 64-bit integer, which makes it a primitive, but limits it's range to those within 64-bit signed/unsigned values.
Int64
A 64-bit signed integer that ranges from -2^63 to 2^63-1.
- TypeScript
- Swift
- Kotlin
- C++
interface Math extends HybridObject<{ … }> {
add(a: Int64, b: Int64): Int64
}
class HybridMath: HybridMathSpec {
func add(a: Int64, b: Int64) -> Int64
}
class HybridMath: HybridMathSpec() {
fun add(a: Long, b: Long): Long
}
class HybridMath : public HybridMathSpec {
int64_t add(int64_t a, int64_t b);
}
UInt64
A 64-bit unsigned integer that ranges from 0 to 2^64-1. On 64-bit systems, this is the most convenient type to describe pointer addresses.
- TypeScript
- Swift
- Kotlin
- C++
interface Math extends HybridObject<{ … }> {
add(a: UInt64, b: UInt64): UInt64
}
class HybridMath: HybridMathSpec {
func add(a: UInt64, b: UInt64) -> UInt64
}
class HybridMath: HybridMathSpec() {
fun add(a: ULong, b: ULong): ULong
}
class HybridMath : public HybridMathSpec {
uint64_t add(uint64_t a, uint64_t b);
}
A UInt64 cannot be stored in an AnyMap.
Anything larger
If you want to represent any bigint larger than UInt64/Int64's maximum value, or smaller than UInt64/Int64's minimum value, you must implement your own big integer type.
You can achieve this either by just passing the bigint as a string and deserializing it again on the native side, or by rolling your own type via Custom Types - in this case the JSIConverter can convert your custom big integer type from- and to a bigint.