leostera / caramel

:candy: a functional language for building type-safe, scalable, and maintainable applications
https://caramel.run
Apache License 2.0
1.06k stars 25 forks source link

erlang: replace unbound vars in specs with wildcards (`_`) (fixes #20) #38

Closed ilya-klyuchnikov closed 3 years ago

ilya-klyuchnikov commented 3 years ago

Fixes #20

Change

Rolling back e9163337e275e895195b58e38986a97a8ebb1687. + Changing the logic of handling type variables in specs:

If a generated/translated spec has a type variable which is used exactly once (erl_lint calls such variables "unbound variables"), such a variable is replaced with the wildcard type variable (_). This makes erlc/erl_lint happy about generated code.

Snapshot tests are updated accordingly.

Subtlety

This new logic doesn't try to distinguish "original" (but also unbound for erlc) OCaml type variables and inferred type variables: if a type variable is unbound it will be replaced by _ anyway.

This makes this example less idiomatic:

let where_is : 'a -> 'message Erlang.pid option =

Before:

-spec where_is(_a) -> option:t(beam__erlang:pid(_message)).

After:

-spec where_is(_) -> option:t(beam__erlang:pid(_)).

Possible improvement(s)


Thanks for a really cool and interesting project!

leostera commented 3 years ago

Thanks for this! It should make it easier to work with type variables going forward 😄

(Merging it now before I start doing more changes to the Erlang Parsetree and it bitrots)