hsutter / cppfront

A personal experimental C++ Syntax 2 -> Syntax 1 compiler
Other
5.43k stars 236 forks source link

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

Open tsoj opened 1 month ago

tsoj commented 1 month ago

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:

farmerpiki commented 1 month ago

I like the idea... but maybe the syntax should be get_underlying and set_underlying ... possibly value as well, so we don't deviate so much from how people are used to using enums and enum classes, where currently you have to cast from/to std::underlying_type_t and you could look for underlying in both cpp1 and cpp2 at the same time to detect potential issues.

I'm curious how others feel about this as well.

DyXel commented 1 month ago

In my opinion, to get the raw value we should just be able to cast, for example, by saying s as u8. To set the raw value, we probably should have a escape hatch yes, but I would like this escape hatch to come both in checked and unchecked flavors, I think maybe a pre-condition that can be optimized away could be good to cover both cases with a single function / constructor.

dwmckee commented 1 month ago

In my opinion, to get the raw value we should just be able to cast, for example, by saying s as u8. To set the raw value, we probably should have a escape hatch yes, but I would like this escape hatch to come both in checked and unchecked flavors, I think maybe a pre-condition that can be optimized away could be good to cover both cases with a single function / constructor.

Being able to get the raw value with as is my suggestion #816, though I would leave the existing get_raw_value method for the pure readability of the thing. And @gregmarr was working on it as part of a larger refactor of as (#701, I think), but that effort seems to have stalled.

My sense is that as was meant to be always safe, so using it for type conversion to @enum type would have to be checked.

DyXel commented 1 month ago

My sense is that as was meant to be always safe, so using it for type conversion to @enum type would have to be checked.

It is always safe to get the underlying value, so the as cast should be safe. The other way around (u8 -> @enum) wouldn't be a cast, but a explicit constructor or a static member function that acts as a constructor.

gregmarr commented 1 month ago

And @gregmarr was working on it

Not me, sorry.

dwmckee commented 1 month ago

And @gregmarr was working on it

Not me, sorry.

Apologies.