Skip to content

[SUGGESTION] Creating an enum instance from a raw integer #1217

Open
@tsoj

Description

@tsoj

In case I didn't miss anything, it is currently not possible to create an enum instance from a raw integer.

If the values of enum instances should matter at all (which is suggested by the possibility to assign custom values to specific enum members, and by the existence of get_raw_value()), then I believe it should also be possible to create an enum instance from an integer.

A specific use case I have in mind:

Square: @enum<u8> type = {
    a1; b1; c1; d1; e1; f1; g1; h1;
    ...
    a8; b8; c8; d8; e8; f8; g8; h8;
    noSquare;
}

flip: (s: Square) -> Square == Square(s.get_raw_value() ^ 56); // e.g. a1 becomes a8 or e5 becomes e4

Ways to make something like this possible could be:

  • Make the enum constructor operator= : (implicit out this, _val: i64) public.
  • A specific static function (Square::from_raw_value(val: i64)) to make clear that converting an integer into an enum is an unusual/dangerous operation
  • Making the underlying value public, which, while being a bit questionable, would also solve [SUGGESTION] Allow @enum classes as template type arguments #1147.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions