Hello guys, can you confirm this is impossible to do with unity ?
classic compiler :
public enum test
{
a,
b,
c,
d= b&e&f,
e,
f,
}
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
var b = test.b;
var bef= test.d;
var e = test.e;
var a =test.a;
if(b == bef)
{
Console.WriteLine("b = bef");
}
if(Enum.Equals( bef==b))
{
Console.WriteLine("bef= b");
}
if(a ==bef)
{
Console.WriteLine("a = bef");
}
}
}
RESULT normal compiler :
b = bef
bef = b
RESULT Unity :
a =bef
What is this ‘normal compiler’ that gives the result you refer to?
I may be wrong, as I’m not up to date with the latest c# behaviour, but the ‘=’ when defining an enum is giving its integer value, so you can compare the enum value using an integer. Whilst the & is a logical “and” (does a bitwise “and”), which would give bef a binary value of 001&100&101 which is 0, which is the same as a. So, from what I can see Unity is giving the answer I’d expect.
So, to expand, I think the enum values would have the integer equivalents of:
a =0 (binary 000),
b =1 (binary 001),
c =2 (binary 010),
d = 001&100&101=0 (binary 000)
e = 4 (or 3?, but will assume 4) (binary 100)
f = 5 (or 4) (binary 101)
Even if e is 3 (011) and f is 4 (100) d would be 0 (000)
Actually, I thought about it a bit more, and couldn’t see how a compiler would cope, so gave it a quick try, and the compiler complained about a circular definition . So it wouldn’t compile (which makes sense).
So I’m not sure what’s going on with your code (how any result is returned).
However I’ve never come across an enumeration with one enum value equivalent to multiple others.
I have found another solution with the flagAttribute, work as i want to
[FlagsAttribute]
public enum TypeResource
{
a = 1,
b= 2,
c = 4,
d = 8,
e = b|c|d,
f= 16
}
class Program
{
static void Main(string[] args)
{
var bcd = TypeResource.e;
var a = TypeResource.a;
var b = TypeResource.b;
var c = TypeResource.c;
var f = TypeResource.f;
Console.WriteLine(a == bcd);
Console.WriteLine(b == bcd);
//false
//false
Console.WriteLine(bcd.HasFlag(a));
Console.WriteLine(bcd.HasFlag(b));
Console.WriteLine(bcd.HasFlag(c));
Console.WriteLine(bcd.HasFlag(f));
//false
//true
//true
//false
Console.Read();
}
}