Skip to content

Latest commit

 

History

History
182 lines (165 loc) · 7.31 KB

File metadata and controls

182 lines (165 loc) · 7.31 KB


import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Nitro's Typing System

Nitro uses an extensible typing system to efficiently convert between JS and C++ types - statically defined and fully type-safe and null-safe at compile-time.

For example, a JS number will always be a double on the native side:

interface Math extends HybridObject {
  add(a: number, b: number): number
}
class HybridMath : public HybridMathSpec {
  double add(double a, double b);
}

Nitro strictly enforces type-safety and null-safety - both at compile-time and at runtime. This prevents accidentally passing a wrong type to add(..) (for example, a string) and performs null-checks to prevent passing and returning null/undefined values.

On the JS side (TypeScript), type- and null-safety is enforced via TypeScript - so use it!

Nitrogen

Nitrogen ensures that TypeScript definitions are always in sync with native type definitions. You can also use Nitro without nitrogen, in this case TypeScript definitions have to be written manually.

Supported Types

These are all the types Nitro supports out of the box:

JS Type C++ Type Swift Type Kotlin Type
number double / int / float Double Double
boolean bool Bool Boolean
string std::string String String
bigint int64_t / uint64_t Int64 Long
T[] std::vector<T> [T] Array<T> / PrimitiveArray
T? std::optional<T> T? T?
[A, B, ...] std::tuple<A, B, ...> (A, B) 🟡  (#38)
A | B | ... std::variant<A, B, ...> Variant_A_B_C Variant_A_B_C
(T...) => void std::function<void (T...)> @escaping (T...) -> Void (T...) -> Unit
(T...) => R std::function<std::shared_ptr<Promise<R>> (T...)> @escaping (T...) -> Promise<R> (T...) -> Promise<R>
Sync<(T...) => R> std::function<R (T...)> @escaping (T...) -> R (T...) -> R
Record<string, T> std::unordered_map<std::string, T> Dictionary<String, T> Map<String, T>
Error std::exception_ptr Error Throwable
Promise<T> std::shared_ptr<Promise<T>> Promise<T> Promise<T>
AnyMap std::shared_ptr<AnyMap> AnyMap AnyMap
ArrayBuffer std::shared_ptr<ArrayBuffer> ArrayBuffer ArrayBuffer
Date std::chrono::system_clock::time_point Date java.time.Instant
..any HybridObject std::shared_ptr<HybridObject> HybridObject HybridObject
..any interface struct T struct T data class T
..any enum enum T enum T enum T
..any union enum T enum T enum T