shadowtalker
shadowtalker

Reputation: 13903

Get the integer value from an enum

I am doing Advent of Code 2022 Day 2, and I have the following enums:

type
  GameMove {.pure.} = enum
    rock = 1, paper = 2, scissors = 3

and I'd like to be able to perform the following calculation:

ours = rock
theirs = scissors
echo ours - theirs

However, I get the error that there is no proc - defined for <GameMove, GameMove>. Indeed there isn't! But how can I define it, and/or how can I get the integer values from this enum in order to perform my calculation?

Upvotes: 0

Views: 216

Answers (1)

shadowtalker
shadowtalker

Reputation: 13903

Converting to int does work, but apparently the correct answer (which is weirdly missing from the language manual) is the ord function, in the system module:

ours = rock
theirs = scissors
echo ours.ord - theirs.ord

Edit: ord is mentioned in the official tutorial, but only in passing, and it's easy to miss or forget!

Upvotes: 4

Related Questions