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

Explore representing atoms using extensible or polymorphic variants #66

Closed
lpil opened this issue Feb 9, 2019 · 3 comments
Closed

Explore representing atoms using extensible or polymorphic variants #66

lpil opened this issue Feb 9, 2019 · 3 comments

Comments

@lpil
Copy link
Member

lpil commented Feb 9, 2019

tomprimozic/type-systems@4403586

@OvermindDL1
Copy link

In OCaml there are both extensible variants and polymorphic variants.

Polymorphic variants with no data are like atoms on the BEAM. In OCaml they are of the form `Head like:

`Blah
`Bloop
`CanAlsoHoldData 42

And they are typed into function as either open typed or closed typed, depending on whether it takes/returns possible extras or a restricted set.

Extensible variants are first given a type name then they can be added to, like:

(* The `..` means it is an extensible variant *)
type blah = ..

(* Add a single head *)
type blah += Str of String

(* Add multiple heads at once *)
type blah +=
| Int of int
| Float of float

I'm thinking the global polymorphic variants are more like BEAM Atoms, though of course a normal data-less variant head of any form would compile down to just an atom anyway.

@OvermindDL1
Copy link

For note, OCaml's Exception type is an extensible variant named exn. You can define a new exception by doing:

type exn += MyException of WhateverData

Since each head+datatype is its own unified type then you can even reuse an existing head if it makes sense for the given exception, like the base Exception Exc:

type exn += Exc of MyModule.MyException.t

That way people can catch it without knowing about it if they have a default datatype handler but don't want to set an overall default handler.

An extensible variant in OCaml can even be defined as private like:

type blah = private ..

That means that only the current module where it is defined (and any modules defined within) can add to it, but they can still be referenced via interfaces.

@lpil lpil changed the title Explore representing atoms using extensible variants Explore representing atoms using extensible or polymorphic variants Feb 11, 2019
@lpil
Copy link
Member Author

lpil commented Feb 11, 2019

Thanks for the info, very useful. OCaml has been a huge influence though I don't know the language in any great detail.

I'm thinking the global polymorphic variants are more like BEAM Atoms

I think you're right and I got my terminology mixed up.

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

No branches or pull requests

2 participants