rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Empty variant type

Open cknitt opened this issue 1 year ago • 5 comments

Follow-up to #6952 where @glennsl wrote:

... In OCaml, an empty variant type would be type a = |. That doesn't seem to be supported in Rescript though.

Do we want to support this in ReScript? Can we use the same syntax?

cknitt avatar Aug 28 '24 16:08 cknitt

What are the real world use cases for empty variants?

zth avatar Aug 28 '24 17:08 zth

What are the real world use cases for empty variants?

https://discuss.ocaml.org/t/empty-polymorphic-variant-type-use-case/8371/2

Very niche actually. It resembles never type in TypeScript in some sense

soanvig avatar Sep 03 '24 09:09 soanvig

Here's the discussion on why it was added to OCaml: https://github.com/ocaml/ocaml/issues/7583

The short summary is that it's seen as less of an addition and more of an omission that was necessitated by how type inference used to work. Since type-based disambiguation was introduced, there is no longer a strong practical reason for disallowing what's seen as a natural base case for variants.

The more practical use cases cited:

  • Since it is a natural base case for variants, many other languages and protocols support it, and it would make interaction with those more natural as well.
  • Combined with refutation cases (which also seem not to be supported syntactically in ReScript, but can also apply implicitly), empty variants can be used in place of type parameters that aren't needed to avoid having to cover unreachable cases by sprinkling assert(false) or the like everywhere.

glennsl avatar Sep 05 '24 19:09 glennsl

@glennsl what's your opinion on its usefulness for ReScript? Is it something you'd like to see supported?

zth avatar Sep 05 '24 19:09 zth

I could definitely see myself using it as I think being able to show in type signatures that a type parameter, and therefore certain parts of an implementation, isn't used or needed is much clearer than hiding that in various parts of the implementation and comments.

I also think it fits quite naturally and the cost is small.

But it's also not something I've missed, so I certainly wouldn't make it much of a priority.

glennsl avatar Sep 05 '24 19:09 glennsl

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Sep 01 '25 02:09 github-actions[bot]