Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 811 Bytes

bs.int.md

File metadata and controls

39 lines (26 loc) · 811 Bytes

[@bs.int]

You can use bs.int together with polymorphic variant to constrain function's parameters.

For example:

[@bs.val]
external log: (
  ~status: [@bs.int] [[@bs.as 200] `ok | [@bs.as 400] `bad_request | `unauthorized],
  ~message: string
) => string = "";

log(~status=`unauthorized, ~message="Not allowed");

compiles to:

log(401, "Not allowed");

bs.int compiled `unauthorized to the number 401.

Now, if we try pass an unknown status:

log(~status=`not_found, ~message="This page is not found");

we'll get a compile error.

Note how we used bs.as to better control the numbers that bs.int compiles to.

If we omitted all the bs.as above, bs.int would compile:

  • `ok to 0
  • `bad_request to 1
  • `unauthorized to 2