Ability to create implicit conversions to int #9588
-
Hi there, in the binder the TokenType for int is 'keyword', which in the Binder's switch case will be replaced with Int32. It seems that the CodeAnalyzor is unaware of this and simply states that the struct Currently, it's impossible to do something like this for implicit enum conversion:
If the code analyzor would allow the compiler to continue if Int32 were equal to the integer keyword, developers would be allowed to implicitly assign enums to integers with something like this partial struct on Int32:
I think this would be a fun feature that developers would enjoy. In many codebases there are a TON of:
while the int conversion could be skipped if the analyzor just knew that the IntegerKeyword would actually become the struct Int32. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
I think this might come for free with extension operators, assuming they cover conversion operators: public static class EnumIntExtensions {
extension(T) where T : System.Enum, struct {
public static implicit operator int(T e) {
return (int) e;
}
}
} Note that not all enums are backed in a 32-bit |
Beta Was this translation helpful? Give feedback.
You are not reading or understanding the responses from several of the designers here.
Your own System.Int32 type is not special. It has no relationship to the actual built-in System.Int32 type (see tanner's comment).
int
maps exactly to that type. Always.--
Aside from that, there are rules for how enums behave (including with the special constant zero value).
Please read the rules in the language here. You keep ignoring the critical information people are taking time to get you to understand.