February 2019
Beginner to intermediate
180 pages
4h 4m
English
Let's try to do this with normal variants and see what happens:
type validArgs = | Int(int) | Str(string);[@bs.val] external dynamic : validArgs => string = "";dynamic(Int(1));
The problem with the preceding implementation is that Int(1) does not compile to a JavaScript number. Normal variants are compiled to an array and our dynamic function returns undefined instead of "Number: 42". The function returns undefined because no cases on the switch statement were matched.
With polymorphic variants, BuckleScript compiles dynamic(`Int(42)) to dynamic(42) and the function works as expected.
Read now
Unlock full access