rescript-lang / rescript-compiler

The compiler for ReScript.
https://rescript-lang.org
Other
6.7k stars 446 forks source link

Provide a type safe primitive to erase types #5012

Closed bobzhang closed 1 year ago

bobzhang commented 3 years ago

It seems this post creates more confusion than it solves: https://rescript-lang.org/blog/union-types-in-bucklescript

Note @unboxed has its own use cases, the idea is to avoid GADT for regular users unless they want to. Assuming we provide a function called erase

let erase : 'a => any

So people can write code like this:

let h = [erase (3),  erase ("3"), erase (#hello)]

This is still type safe and useful when you want to pass a heterogenous list of objects to the js user side, suggestions on the name of function and types?

TheSpyder commented 3 years ago

I've seen many people attempt to use unboxed and struggle with it (both understanding what it does and how to use it).

erase doesn't seem particularly friendly... maybe any?

ryyppy commented 3 years ago

I am highly in favour for not adding any primitive for that just yet, until we finally figured out how to proceed with the whole stdlib namespace cleanup discussion (Js vs Js2,..).

Can't we just create a doc in the interop section that shows how to export heterogenous arrays by introducing your own erase binding in userspace? It doesn't look particularly hard, and adding an any type or function in the core just makes it look weird from a marketing perspective.

bobzhang commented 3 years ago

@ryyppy this could certainly be done in user space and it is trivial. It is like option or result type, which can be done in user land, but you need a standard option type so people understand each other. any may be not a good name since it means type hole while it is not the case here, opaque? This is planned to be added into pervasives so should not be in conflict with Js/Js2

chenglou commented 3 years ago

Since this is for JS interop, it kinda does make sense to put this under Js (or Js2) right? Easier to name and explain too. E.g. Js.opaque vs opaque the latter of which sounds misleadingly general

bobzhang commented 3 years ago

the concept is backend agnostics. It makes sense in native as well when you want to pack values of different types into a data collection

stale[bot] commented 1 year ago

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.