How to get the variant type, not the payload?

Is there a more clear way in rescript to get the variant type, not the payload? Now I use pattern matching to do this. Maybe more graceful solution exist?

type component =
  | Position(Vector2.t)
  | Velocity(Vector2.t)

…more variants

let isPosition = component => {
  switch component {
  | Position(_) => true
  | _ => false
  }
}

let isVelocity = component => {
  switch component {
  | Velocity(_) => true
  | _ => false
  }
}

…more functions like above

As far as I know this is the only way.

I have a pattern of making a second Enum type and toEnum then you can atleast do the comparisons more literally

type enum = Position | Velocity
let toEnum = (c: component): enum => {
  switch c {
   | Position(_) => Position
   | Velocity(_) => Velocity
}

I did that at first, too. Later I needed to get components by type from an array, so I used the functions from the above.

I was expecting to find a syntax that looks like this:
Velocity({x: 0.03, y: 0.09}) == Velocity(_)

There isn’t a language-level way of doing this. But I’m wondering, what’s your motivation for needing it?

In my opinion, code like this:

if isPosition(x) {
  // do something
} else {
  // do something else
}

Doesn’t have any advantage over this:

switch x {
| Position(_) => // do something
| Velocity(_) => // do something else
}

For cases where you do need a boolean for some reason, then it’s simple to define a function like isPosition manually.

I can use functions of type isComponentName with arrays of components or other sets.
Something like this:

let cArray = [Position({x: 130.6, y: 200.2}), Velocity({x: 0.015, y: 0.01})]
let index = cArray->Belt.Array.getIndexBy(isVelocity)
let exist = cArray->Belt.Array.some(isVelocity)

Maybe I don’t understand something? :slightly_smiling_face: