Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type aliasing #34

Closed
danabr opened this issue Aug 23, 2016 · 0 comments · Fixed by #49
Closed

Type aliasing #34

danabr opened this issue Aug 23, 2016 · 0 comments · Fixed by #49

Comments

@danabr
Copy link
Contributor

danabr commented Aug 23, 2016

Consider the following example:

module shape

type radius = int

type shape = Circle radius

make_circle r = Circle r

test_circle () = make_circle 1

This unexpectedly fails to typecheck with the error:

exception error: no match of right hand side value 
                 {error,{cannot_unify,shape,9,{adt,"radius",[],[]},t_int}}

That is, radius is considered a distinct type from int.

The same module in OCaml compiles just fine:

type radius = int

type shape = Circle of radius

let make_circle r = Circle r

let test_circle () = make_circle 1

I think it makes sense for MLFE to behave the same way.

If I would like to make the radius type abstract (or opaque in dialyzer terms), I would hide it in a module (example in OCaml):

module Radius : sig
  type radius

  val make_radius: int -> radius
end =
struct
  type radius = int

  let make_radius i = i
end

type shape = Circle of Radius.radius

let make_circle r = Circle r

let test_circle () = make_circle (Radius.make_radius 1)

To achieve the same in MLFE, types should be module local by default, and you would have to export them via a export_type directive or similar. We would also need to be able to mark types as abstract. In Erlang/Dialyzer this is achieved by using the -opaque directive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant