vrchat-community / UdonSharp

A compiler for compiling C# to Udon assembly
https://udonsharp.docs.vrchat.com
MIT License
462 stars 50 forks source link

Enum types use enum value rather than enum name when converted to string #71

Open synergiance opened 1 year ago

synergiance commented 1 year ago

Describe the bug in detail: In C#, when you use ToString() on an enum type, it will return the name of the enum value, while in U# it returns the integer converted to a string. This is undesirable behavior most of the time, and unaligned with C#'s behavior.

Provide steps/code to reproduce the bug: Let's take this enum for instance:

public enum MyEnum {
    MyFirstValue,
    MySecondValue
}

If we print it out like this, we should see the problem:

MyEnum myEnumInstance = MyEnum.MyFirstValue;
Debug.Log(myEnumInstance.ToString());

Expected behavior: Log should read "MyFirstValue"

Observed behavior: Log actually reads "0"

MerlinVR commented 1 year ago

This is known and will have partial handling eventually but can't have full handling due to boxing obscuring the user type.

jellejurre commented 1 year ago

Maybe add it to the "Differences from regular Unity C# to note" list at https://udonsharp.docs.vrchat.com/?

synergiance commented 1 year ago

Currently I work around it by creating a string array containing the names of the enum values.

Unfortunately though the workaround splits these into two separate parts of the code, which is undesirable. Is it possible to just internally create this structure and any enum tostring just reference the name array for that enum?

MerlinVR commented 1 year ago

Yeah that's the plan to generate arrays, you can make a PR for it if you want it before I get around to it. If you do, note that enums with the Flags attribute generate their ToString() value differently from ones without it.

synergiance commented 1 year ago

Speaking of enums using the flags attribute, it appears that operator | confuses the symbol processor on flags enums.

MerlinVR commented 1 year ago

Only equality and inequality are handled for enums at the moment