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

Require Qualified Access for Variants

$
0
0

So, I have something like

@unboxed
type facing = | @as(true) Up | @as(false) Down

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

Which I really like because in my case I need to know if an object is Up or Down, a clear binary value.
But I never really need this for any boolean logic. So having English words for this is really beneficial.

Anyway, Up and Down are very generic.
In F# you can use an attribute RequireQualifiedAccess to mark your type.
With that you can’t use Up or Down without specifying its owning type.

Normally, the case identifiers can be used without qualifying them with the name of the union. If you want the name to always be qualified with the name of the union, you can apply the RequireQualifiedAccess attribute to the union type definition.

In F# this would be:

[<RequireQualifiedAccess>]
type Direction = | Up | Down

// usage
let x = Direction.Up
let y = Down // won't work, compiler blocks it

What is in ReScript the closest I can get to this behaviour?


Viewing all articles
Browse latest Browse all 2592

Trending Articles