[BuckleScript 8.1 New Syntax]: Polymorphic variants


#1

With # replacing the backtick for polymorphic variants I wonder how will the following translate to the new syntax?

type nums = [ | `one | `two | `three];

type alphas = [ | `a | `b | `c];

type alphanums = [ nums | alphas ];

let a = `a;

switch(a) {
  | #alphas => Js.log("alphas")
  | #nums => Js.log("nums")
};

My assumption is backticks were replace with # to allow for backticks to be used for multiline strings. I’m not arguing with the switch, just wondering how to deal with the scenarios like the one above


#2

This compiles:

type nums = [ | #one | #two | #three];

type alphas = [ | #a | #b | #c];

type alphanums = [ nums | alphas ];

let a: alphanums = #a;

switch a {
| ##alphas => Js.log("alphas")
| ##nums => Js.log("nums")
};

#3

hm… this isn’t that bad actually. Thank you!


#4

I don’t get the point of having extra leading # on patterns.


#5

You mean you don’t get why it’s needed or what it does?

If the latter, ##alphas matches any of [ | #a | #b | #c] constructors. It’s equivalent to | #a | #b | #c => ....

If the former, with single # it would be a polymorphic constructor, Ppat_variant in AST. VS Ppat_type in case of ##.


#6

Oh you’re right


#7

There’s been a proposal in https://github.com/BuckleScript/syntax/issues/7 to make the poly variant abbreviations cleaner.
Implementation is pretty simple, here: https://github.com/BuckleScript/syntax/pull/11

Instead of writing:

| #Rectangle(_, _) as r => computeArea(r)
| #Circle(_, _) as c => computeArea(c)

you can use the following syntax:

| #...shape as s => computeArea(s)