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

Union types don't output correctly #5

Closed
rrdelaney opened this issue Jun 21, 2017 · 2 comments
Closed

Union types don't output correctly #5

rrdelaney opened this issue Jun 21, 2017 · 2 comments
Projects

Comments

@rrdelaney
Copy link
Owner

Union types don't output correctly at all in compiled JS - we need a new approach to remove the tag from a variant. Here's what I cam up with in the BS playground today:

type union_of_string_or_bool =
  | String of string
  | Bool of bool
    
type string_or_bool

external string_or_bool : union_of_string_or_bool -> string_or_bool = "Array.prototype.shift.call" [@@bs.val];;

type union_of_string_or_float =
  | String of string_or_bool
  | Float of float
    
type string_or_float

external string_or_float : union_of_string_or_float -> string_or_float = "Array.prototype.shift.call" [@@bs.val];;
    
external log : string_or_float -> unit = "" [@@bs.val "console.dir"];;

log (string_or_float (String (string_or_bool (Bool true))))

I'm pretty sure this could work, and it would cover all cases of union types.

@saschatimme
Copy link

If you could also hide the string_or_float call:

external _log : string_or_float -> unit = "" [@@bs.val "console.dir"];;
let log union_of_string_or_float = _log (string_or_float union_of_string_or_float)

log (String (string_or_bool (Bool true)))

This wouldn't cover the case of nested union, though. Do these occur at all?

@rrdelaney
Copy link
Owner Author

More often than I'd like them to. Another problem is when the union is declared outside of the parameter than the type is substituted

@rrdelaney rrdelaney added this to Done in Compiler Aug 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants