Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.35 KB

File metadata and controls

56 lines (42 loc) · 1.35 KB


Variants (A | B | ...)

A Variant is a type of either one of the values defined in it's declaration. Example:

interface Math extends HybridObject {
  distance(value: number | Point): number
}

:::tip While variants are still very efficient, they need runtime-checks for type conversions, which comes with a tiny overhead compared to all other statically defined types. If possible, avoid variants. :::

Custom Alias Names

Each variant is a unique type in Swift/Kotlin - for example: string | number becomes Variant_String_Double.

Since the generated names are hard to read, it is recommended to declare type-aliases with custom names instead:

export interface Math extends HybridObject {
  calculate(): string | number
}
export type MathOutput = string | number
export interface Math extends HybridObject {
  calculate(): MathOutput
}

This will then use the easier-to-read type-alias name instead of Variant_String_Double:

public protocol HybridMathSpec: HybridObject {
  // diff-remove
  func calculate() -> Variant_String_Double
  // diff-add
  func calculate() -> MathOutput
}