Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Require Qualified Access for Variants

$
0
0

There are a few things you can do, the simplest being to just choose longer variant names:

@unboxed
type facing = | @as(true) FacingUp | @as(false) FacingDown

let f = v => {
  switch v {
  | FacingUp => Console.log("up")
  | FacingDown => Console.log("down")
  }
}

You can of course put it in a module and always prepend the module name as @zth explained!

If you want to add another layer of security, you could also declare it as private and create helper functions to create such variants:

module Facing: {
  @unboxed
  type t = private | @as(true) Up | @as(false) Down
  let up: t
  let down: t
} = {
  @unboxed
  type t = | @as(true) Up | @as(false) Down
  let up = Up
  let down = Down
}

This way, you can enforce that you have to use the module name to produce such values.


Viewing all articles
Browse latest Browse all 2592

Trending Articles