Whats wrong with this C# compiler :D Can you guess the OBVIOUS mistake on Eric Gunnarsson's part.

M

Mr.Tickle

Why would I ask you, when I can ask Jack as Im sure he knows more than you.


If I wanted Jack's opinion I would ask Jack.
 
1

100

Hi,
Flag attributes is used only by the methods that converts the value to text
(ToString method).
You can use bitwise oberators even if FlagAttribute is not set.
There is no restriction that FlagAttribute has to be used only with powers
of 2.

B\rgds
100
 
N

NoOne

You started with "obvious error by the compiler", "Obvious mistake by Eric
Gunnarsson", "should be powers of two". After people pointed out that the
flag attribute is doing exactly what the spec says. You now changed your
tune to "I'm thinking outside the box" and "wouldn't it be handy to
increment of powers of 2". Which indicates you had no idea what the flag
attribute should have been doing to begin with, else you would have started
out the thread with "would be useful to specify an increment of powers of
2".
 
M

mikeb

NoOne said:
You started with "obvious error by the compiler", "Obvious mistake by Eric
Gunnarsson", "should be powers of two". After people pointed out that the
flag attribute is doing exactly what the spec says. You now changed your
tune to "I'm thinking outside the box" and "wouldn't it be handy to
increment of powers of 2". Which indicates you had no idea what the flag
attribute should have been doing to begin with, else you would have started
out the thread with "would be useful to specify an increment of powers of
2".

Indeed. I actually think Mr. Tickle's idea is a good one. There is
ample precedent for attributes modifying compiler behavior.

However, I think that his name-calling approach of attempting to get his
point across is certainly counter productive.

Oh well, it's not like this is a gaping hole in the C# spec.
// Run this code and look for his obvious error by the compiler


namespace FlagCheck
{
using System;

[Flags]
enum SomeFlags
{
None = 0,
One,
Two,
Three,
Four,
Five,
Six,
All = One | Two | Three | Four | Five | Six
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
SomeFlags f;

f = SomeFlags.Four; // Set flag FOUR and ONLY FOUR, get ready for a
supprise

if ( (f & SomeFlags.One) > 0)
{
Console.WriteLine("flag one is set");
}

if ( (f & SomeFlags.Two) > 0)
{
Console.WriteLine("flag two is set");
}

if ( (f & SomeFlags.Three) > 0)
{
Console.WriteLine("flag three is set");
}

if ( (f & SomeFlags.Four) > 0)
{
Console.WriteLine("flag four is set");
}

if ( (f & SomeFlags.Five) > 0)
{
Console.WriteLine("flag five is set");
}

if ( (f & SomeFlags.Six) > 0)
{
Console.WriteLine("flag six is set");
}
Console.ReadLine();
}
}
}
 
1

100

Hi Mr.Tickle.
To have powers of to as defalut for flags doesn't make sense to me.
Usualy flags provide constants often used combination of flags.
For example

enum Styles
{
WS_EX_WINDOWEDGE = 0x00000100L,
WS_EX_CLIENTEDGE = 0x00000200L,
WS_EX_OVERLAPPEDWINDOW = 0x00000300
}

WS_EX_OVERLAPPEDWINDOW (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE) = 768.
is it power of 2?

Some of the flags might have more the one bit set to 1 and thay might be not
power of 2. This doesn't makes them *less flags* than the others, though.

B\rgds
100
 
F

Fabian Schmied

However, I think that his name-calling approach of attempting to get his
point across is certainly counter productive.

The usual advice in such cases is "Do Not Feed The Troll". I think this most
definitely applies here.
 
A

Alan Pretre

100 said:
Some of the flags might have more the one bit set to 1 and thay might be not
power of 2. This doesn't makes them *less flags* than the others, though.

Right. So the only sure way is to explicitly set the value, which is a good
practice anyway. I don't know how the compiler could be psychic and know
what the author intends. Somehow specifying a nonlinear increment sounds
like trouble in the long run.

-- Alan
 
A

Alan Pretre

Mr.Tickle said:
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.

Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}
 
B

Bruno Jouhier [MVP]

Nothing's wrong with the compiler. It behaves according to specs.

It would have been more accurate to say: "what's wrong with the C# language
.....".

IMO, the [Flags] attribute is not a very good design. The difference between
"normal" enums and "flags sets" is a rather fundamental one (from a semantic
viewpoint, not an implementation one) and it should be captured by a
language construct (i.e., a different declaration keyword) rather than by an
attribute. Then, it would be possible to have their numeric values be powers
of two by default. With the current design (the attribute hack), this would
be problematic because attributes are not supposed to influence things like
this (at least the way I understand them).

Bruno.
 
M

Mr.Tickle

if you need more than one bit set , override the default like i did in the
All = one | blah example. just as normal.

Have you ever seen a bit set to 3 by default? thats what it does today,
default behaviour on bitfields should be powers of 2, if you want otherwise
(like enums normally do) override it with a value, then it continues from
there.



Alan Pretre said:
Mr.Tickle said:
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.

Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}
 
M

Mr.Tickle

Normally when you have multiple bits set, you set them from the previous
one, like the "All" exmaple is an | of the other values. Youre argument by
setting it to 6 isnt valid, set it to the a = whatever the combination of
bits is which is individually named...


Alan Pretre said:
Mr.Tickle said:
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.

Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}
 
M

Mr.Tickle

we are talking DEFAULT assignments to fields that arnt overriden in value,
not forced onto all values. go figure youre argument, you have none.




Alan Pretre said:
Mr.Tickle said:
Thats my point, it should be. There is no reason to have it otherwise.
Logic dictates that it should be powers of 2, nothing else.

Using what you propose, what would be the value of b here?

[Flags]
enum Flags {
a = 6,
b
}
 
A

Alan Pretre

Mr.Tickle said:
if you need more than one bit set , override the default like i did in the
All = one | blah example. just as normal.

That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.
Have you ever seen a bit set to 3 by default?

Whether I have or not is irrelevant. The question is, is it possible? The
answer is yes. In the general case bitfield constants can't be constrained
to single 1-bits only.
default behaviour on bitfields should be powers of 2, if you want otherwise
(like enums normally do) override it with a value, then it continues from
there.

"Then it continues from there". How? There is no increment that is
appropriate for all bitfield cases (not even +1 as you've shown, but neither
will 2^n), so explicitly setting the constants is really the only sure way.

-- Alan
 
A

Alan Pretre

Alan Pretre said:
That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members are explicitly initialized.

Typo, I should have said,

That's right, you have to explictly set it. You're arguing about what C#
should do when the enum members **aren't** explicitly initialized.

-- Alan
 
M

Mr.Tickle

No but im talking DEFAULT behaviour, jesus get it thru yer thick skull.
defaults are set to multi bits set . thats the issue.
 
M

Mr.Tickle

Im not talking about CONSTRAINING them im discussing DEFAULT values for non
set items. Dumbfuck.
 
M

Mr.Tickle

Well if we have to EXPLICITLY set it, then the compiler is introducing
errors by not enforcing hits EXPLICID setting.

If its not explicidly set then its setting BY DEFAULT multiple bits. Im
proposing that DEFAULT for UNASSIGNED values are in powers of 2, which would
then be single set bits for UNASSIGNED values.
 
A

Alan Pretre

Mr.Tickle said:
If its not explicidly set then its setting BY DEFAULT multiple bits. Im
proposing that DEFAULT for UNASSIGNED values are in powers of 2, which would
then be single set bits for UNASSIGNED values.

But the way enums get set by default depends on the member's value
immediately before. You haven't yet said how you would determine an
appropriate default value for a member when the preceding value is not an
exact power of two. Now I'm wondering, can you converse without
profanities? And what is your name anyway?

-- Alan
 
J

Jason Smith

Mr. Tickle, you need to learn what a bit is.

All integers are represented as bits.

The number 3 sets two bits. The number 7, 3 bits.

There is nothing to say that this isn't the way you intended it to work,
unless you say this isn't the way you intended it to work. Now that you
know how it works, and that it works according to design, quit yer griping
and get on with life, or go work with Java, which doesn't have enumerations.

Mr.Tickle said:
Thats my point, it should be. There is no reason to have it otherwise.

Logic dictates that it should be powers of 2, nothing else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top