prakhar1989/JSJS

Generics

Closed this issue · 0 comments

Strategy for type-checking generics

Decisions
  1. For generic functions at the time of declaration, only typecheck signature and not bodies.
  2. Calls to generic functions, requires concrete type-resolution to be done apriori.
  3. Cannot pass generic functions are arguments.
val num_to_sign = /\(x: num): string => {
  if (x >= 0) then "non-negative" else "negative";
};

val bool_to_string = /\(x: bool): string => {
   if x then "true" else "false";
};

val apply = /\[T, U](fn: (T) -> U, x: T): U => {
  fn(x);
};

val is_positive = apply(num_to_sign, -10);
val to_string = apply(bool_to_string, 10 == 10);