Proposal: new first-class ValueType entity — Order/Hierarchy #9473
Replies: 3 comments 2 replies
-
I'm confused. You're proposing not just nested enums, but an And to beat a dead horse: your use case feels extremely niche and would be easily served with a JSON input to a source generator. |
Beta Was this translation helpful? Give feedback.
-
If the objective is just the behavior of an enum, just with nested definition syntax, I don't think it deserves a feature. Instead, an SG can be used to write this: public class OrderRoot : Attribute { }
public class OrderNesting<T> : Attribute where T : struct, Enum { }
public enum _SingleHanded
{
Sword,
Wand,
Knife,
[...]
}
public enum _Weapon
{
[OrderNesting<_SingleHanded>]
SingleHanded,
[OrderNesting<_TwoHanded>]
TwoHanded
}
[OrderRoot]
public enum _GameItemType
{
[Nesting<_Weapon>]
Weapon,
}
// SG generates:
public enum GameItemType
{
Weapon_SingleHanded_Sword,
Weapon_SingleHanded_Wand,
Weapon_SingleHanded_Knife,
// ... etc. ....
} |
Beta Was this translation helpful? Give feedback.
-
This seems like a natural extension to type unions where one would allow pattern-matching to a union-of-unions recursively. enum SingleHanded {}
enum TwoHanded {}
enum Food {}
enum Tonic {}
enum Clothing {}
union Weapon(SingleHanded, TwoHanded)
union Consumable(Food, Tonic)
union GameItemType(Weapon, Consumable, Clothing)
GameItemType x;
switch(x) {
case SingleHanded: /* matches GameItemType.Weapon.SingleHanded */
case Weapon: /* matches GameItemType.Weapon.* */
case Clothing: /* matches GameItemType.Clothing */
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My idea is similar to #587, but with several exceptions.
I want to propose a new data type which represents single node from tree-like structure. It can be helpful to use as a self-explaining marker in complex systems where at some level of abstraction different objects are represented by the same class. It can be solved by using enum, but it is a "linear" type (in a sense all its possible values are of the same order), so additionally can be added attributes, but their managing can become too complex.
As you can see order can represents different levels of nestedness. Also objects of the same type are compared by their full values, not the terminal one.
You can create order object only if its last element is a Terminal. All nodes without children are Terminal. But you can explicitly mark a node with children as a Terminal with '?' symbol.
Even the very type as an entry node can be marked as a Terminal:
Possible implementation:
OrderNode is a core struct, which contains information about single separate node of the type, including its name, children and is it Terminal node or not.
Should be generated new struct with the user-defined name, where static constructor generates code, which creates all nested nodes based on parsed user-defined structure of a type.
So newly created type provides ability to check in runtime all inner structure of a type. You can set separator symbol which will affect comparison of objects. In compile time the parser should read initialization of order variable as a string, e.g.
split it by separator and pass into the constructor.
Well, it's not enum because of nested nature, it converts to a string by default and there is no casting to numeric values.
Beta Was this translation helpful? Give feedback.
All reactions