How to use the @as decorator?

I am experimenting with the @as decorator with the following code:

type status = [
  @as("GOOD") #Good |
  @as("BAD") #Bad

@val external setStatus: status => unit = "setStatus"


This compiles to:


But I was expecting it would compile to:


I suspect I am not using the decorator correctly. Any suggestions on how to correct this?

View in playground


After some more investigation I’ve discovered this:

@val external setStatus: @string[
  @as("GOOD") #Good |
  @as("BAD") #Bad
] => unit = "setStatus"


Which produces:


So it seems that:

  1. It’s intended for use with externals, and
  2. the @string decorator is also required.

How does that sound?


Some more investigation, I see it can be used with record types as well.

type action = {@as("type") type_: string}

let one: action = {type_: "One"}

which produces:

var one = {
  type: "One"

So it seems, that the @as decorator:

  1. may be used with record types directly, and
  2. may be used with polymorphic variants on externals with the @string or @int decorators.

How does that sound?

Your two statements are correct.

Poly vars are more flexible with the identifier names for its constructors, so you can easily do it without the as decorator as well, e.g. #GOOD.

Docs on poly vars are currently in the last review phase, so hopefully this will be more clear in the future!