Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1948

Confusion regarding generic type function definition inside of another function

$
0
0

It means that the type is inferred when the top level function, main is invoked. Not when trace is invoked (when defined inside main). You can think of it as main having a hidden type parameter:

let main = (_: 'a) => {
  let trace = (x: 'a, b: string) => ...
  ...
}

The 'as here refer to the same type variable, and its concrete type will therefore need to be determined when main is invoked.

Because the type variable hasn’t been named. I can only speculate on why it works like that, but my guess would be that without a name it can’t be referred to in main and its type can therefore be inferred independently.

I don’t know what “specific scope” means. The real source of truth for language semantics is usually the OCaml manual, since ReScript is based on OCaml:

In general the scope of a named type variable is the whole top-level phrase where it appears, and it can only be generalized when leaving this scope. Anonymous variables have no such restriction. In the following cases, the scope of named type variables is restricted to the type expression where they appear: 1) for universal (explicitly polymorphic) type variables; 2) for type variables that only appear in public method specifications (as those variables will be made universal, as described in section ‍11.9.1); 3) for variables used as aliases, when the type they are aliased to would be invalid in the scope of the enclosing definition (i.e. when it contains free universal type variables, or locally defined types.)

https://ocaml.org/manual/5.2/types.html#sss:typexpr-variables


Viewing all articles
Browse latest Browse all 1948

Trending Articles