-
Notifications
You must be signed in to change notification settings - Fork 34
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
Support Sub-set to Super-set casts for Tagged Union #439
Comments
[<Emit("$0")>] static member op_ErasedCast(x: CircleOrSquare) : Shape = jsNative For [<Emit("$0.kind")>] member x.kind : string = jsNative then do: let asCircleOrSquare (s: Shape) : CircleOrSquare option =
if (s.kind == "square" || s.kind == "circle") then
Some !!s
else
None I guess this will result in a more performant JS output. Also I think it needs |
I like explicit matching -- even when completely erased at compile time. But of course: one case ctor is easier to do than a match with lots of cases -> longer source and potential more complex to read. Directly just passing the js object along without any matching is of course easier to implement -> I think we should do that I prefer
Doesn't the match boil down to exactly that? // match
export function asCircleOrSquare(s) {
switch (s.kind) {
case "circle": {
const c = s;
return some(c);
}
case "square": {
const s_1 = s;
return some(s_1);
}
default: {
return void 0;
}
}
}
// kind
export function asCircleOrSquare2(s) {
if (((s.kind) === "square") ? true : ((s.kind) === "circle")) {
return some(s);
}
else {
return void 0;
}
} (Don't know which exactly is faster (probably get reduced to same? JS interpreters are quite clever nowadays). Though if there's a speed difference I think it's negligible). I which case does directly using |
👍
Ah, I can see |
=>
=> 'Shape is superset of CircleOrSquare' is lost
-> Add additional
op_ErasedCast
to convert fromCircleOrSquare
toShape
:(see #438 (comment))
Maybe an additional conversion from
Shape
toCircleOrSquare
?Typescript can detect matching subset:
In F# maybe some (explicit) cast function?
(fable repl)
F# function in
Shape
module? member onShape
? member onCircleOrShape
(probably not ->CircleOrShape
sub-set inShape
, not part ofShape
inCircleOrShape
decl)? Probably needstry
in name (tryAs...
)The text was updated successfully, but these errors were encountered: