-
Notifications
You must be signed in to change notification settings - Fork 442
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
Ideas: allow users to use GADT for typing externals #686
Comments
The flow typed repo is barer than the typescript counterpart. If we can somehow kill two birds with one stone by using and/or generating flow types that'd be a big win for both the js and the ocaml folks. Imagine js folks converting to ocaml and BS because they were already using flow-typed! |
I reopened #637 about discussing generating .d.ts files for flow, this issue is about GADT : ) |
I'd like to see a complete (small) example of the difference between poly variants and GADTs (you mentioned extensible but I don't see any |
@bobzhang This looks very interesting! I would really love to have GADT support. FWIW, there some experimental GADT support in However, I have used a kind of hack for GADTs in my bucklescript Electron library. I use it for defining event types here. Then see it used here. The What GADTs bring to the table that poly variants don't is the able to define event types that support subtyping. So in my example, The way it actually gets implemented is by using some metadata that I store alongside the file here. Then I wrote a babel plugin that does the post-processing. It's not a perfect solution but it does work very well. |
@freebroccolo thanks for your feedback. note that |
@bobzhang If I'm understanding correctly, I think it could still be useful even if restricted to I'm not sure if intersection-style typing like that is enough on its own to justify supporting GADTs yet. But maybe the unification you mention with poly variants would be worth it. By the way, I know that hack I described above (with The basic idea is to use extensible variants (which could be GADTs) and provide some sort of metadata (maybe using In my example above I am only translating them to JS |
@freebroccolo I have another idea to make use of GADT which seems more solid and simple: function add (x,y){
return x + y
} It works with both type _ kind =
| Float : float kind
| String : string kind
external add : ('a kind [@bs.ignore]) -> 'a -> 'a -> 'a = "" [@@bs.val] Here so user can write code let u : float = add Float 3.0 2.0
let v : string = add String "x" "y" var u = add (3.0,2.0)
var v = add ("x","y") in your case, you can design a wrapper like this external add : ('a kind [@bs.ignore]) -> string -> 'a -> 'a -> 'a = "" [@@bs.val]
let add k a b =add k (string_of_kind k) a b This is different from gen_js_api since it will still be as efficient as you wish? |
@bobzhang this seems quite nice. It does look like it would probably work for most of the cases I had in mind. I guess this would just work without any issues with extensible variant GADTs too? |
yes, simple and easy to maintain |
We already allows users to use polymorphic varaiant for typing FFI, which can model some
dependent typed semantics. A more powerufl approach is to use GADT(extensible variants)
for this too. The technical challenging part is that we need see the type definition to generate readable code, below is my proposal, which I think is doable
The syntax is a bit heavy,
question: how much would it be useful to type existing JS libraries?
CC @freebroccolo
The text was updated successfully, but these errors were encountered: