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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan Pretre

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

    "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
    news:%23bXMSEnk DHA.1808@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Sure you can override it if you want other values, just like normal
    > behaviour today. but having an option to have it enumerate in powers of 2
    > would be handy as in my example. Isnt that the point of Enums, to
    > enumerate, in this case it enumerats incorrectly for bitfields on a base 2
    > system.[/color]

    The C# language spec says, in 14.3:

    "...the associated value of the enum member is obtained by ___increasing
    the associated value of the textually preceding enum member by one___. This
    increased value must be within the range of values that can be represented
    by the underlying type; otherwise, a compile-time error occurs. "

    As Patrick says, various CLR languages may treat the attribute differently.
    In my tests I couldn't see any difference in behavior with the
    FlagsAttribute in C# (what I mean is whether present or not, C# seemed to
    function the same.)

    To return to your OP, I think the only one surprised was you.

    -- Alan


    Comment

    • Jon Skeet [C# MVP]

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

      Alan Pretre <no@spam> wrote:[color=blue]
      > Why don't you RTM and see what it does. Does it say that the FlagsAttribute
      > changes the way enum constants are defined? Hmmm? No it causes the
      > compiler to relax the restrictions on the bitwise OR operator, which would
      > otherwise not be allowed on the enum members.[/color]

      In fact, it doesn't even do that - you can use the bitwise OR operator
      even on enumerations which *don't* have the [Flags] attribute - at
      least you can in C#.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Mr.Tickle

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

        No i wasnt supprised. I was justing questioning the status quo, why must it
        be inrements of ONE in the standard, why not have this definable in the
        attribute. Then it would be useful to specify an increment of powers of 2
        for bitfields, I know what the specs say, I am just trying pointing out a
        case where it would be handy to have a different increment. Why must always
        the lame people that cant think out of the box always run off to the manuals
        for backup quotes. Try thinking out of the box. I guess you are one of
        those people that never actually made a DIFFERENCE; you are just a
        FOLLOWER.Nuff said really.



        "Alan Pretre" <no@spam> wrote in message
        news:e2Gj7LnkDH A.3504@TK2MSFTN GP11.phx.gbl...[color=blue]
        > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
        > news:%23bXMSEnk DHA.1808@TK2MSF TNGP09.phx.gbl. ..[color=green]
        > > Sure you can override it if you want other values, just like normal
        > > behaviour today. but having an option to have it enumerate in powers of[/color][/color]
        2[color=blue][color=green]
        > > would be handy as in my example. Isnt that the point of Enums, to
        > > enumerate, in this case it enumerats incorrectly for bitfields on a base[/color][/color]
        2[color=blue][color=green]
        > > system.[/color]
        >
        > The C# language spec says, in 14.3:
        >
        > "...the associated value of the enum member is obtained by ___increasing
        > the associated value of the textually preceding enum member by one___.[/color]
        This[color=blue]
        > increased value must be within the range of values that can be represented
        > by the underlying type; otherwise, a compile-time error occurs. "
        >
        > As Patrick says, various CLR languages may treat the attribute[/color]
        differently.[color=blue]
        > In my tests I couldn't see any difference in behavior with the
        > FlagsAttribute in C# (what I mean is whether present or not, C# seemed to
        > function the same.)
        >
        > To return to your OP, I think the only one surprised was you.
        >
        > -- Alan
        >
        >[/color]


        Comment

        • Alan Pretre

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

          "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
          news:uT7qZOnkDH A.2592@TK2MSFTN GP10.phx.gbl...[color=blue]
          > I guess you are one of
          > those people that never actually made a DIFFERENCE; you are just a
          > FOLLOWER.Nuff said really.[/color]

          LOL you are new around here. Just ask me about deterministic destructors.

          -- Alan


          Comment

          • Mr.Tickle

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

            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.
            "Alan Pretre" <no@spam> wrote in message
            news:uznGNQnkDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
            > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
            > news:uT7qZOnkDH A.2592@TK2MSFTN GP10.phx.gbl...[color=green]
            > > I guess you are one of
            > > those people that never actually made a DIFFERENCE; you are just a
            > > FOLLOWER.Nuff said really.[/color]
            >
            > LOL you are new around here. Just ask me about deterministic destructors.
            >
            > -- Alan
            >
            >[/color]


            Comment

            • Mr.Tickle

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



              If you cant see the application of specifying incrementts in enumerations,
              you need glasses.


              "Alan Pretre" <no@spam> wrote in message
              news:uznGNQnkDH A.1408@TK2MSFTN GP11.phx.gbl...[color=blue]
              > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
              > news:uT7qZOnkDH A.2592@TK2MSFTN GP10.phx.gbl...[color=green]
              > > I guess you are one of
              > > those people that never actually made a DIFFERENCE; you are just a
              > > FOLLOWER.Nuff said really.[/color]
              >
              > LOL you are new around here. Just ask me about deterministic destructors.
              >
              > -- Alan
              >
              >[/color]


              Comment

              • 100

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

                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


                Comment

                • NoOne

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

                  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".


                  "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                  news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=blue]
                  > // 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.WriteLi ne("flag one is set");
                  > }
                  >
                  > if ( (f & SomeFlags.Two) > 0)
                  > {
                  > Console.WriteLi ne("flag two is set");
                  > }
                  >
                  > if ( (f & SomeFlags.Three ) > 0)
                  > {
                  > Console.WriteLi ne("flag three is set");
                  > }
                  >
                  > if ( (f & SomeFlags.Four) > 0)
                  > {
                  > Console.WriteLi ne("flag four is set");
                  > }
                  >
                  > if ( (f & SomeFlags.Five) > 0)
                  > {
                  > Console.WriteLi ne("flag five is set");
                  > }
                  >
                  > if ( (f & SomeFlags.Six) > 0)
                  > {
                  > Console.WriteLi ne("flag six is set");
                  > }
                  > Console.ReadLin e();
                  > }
                  > }
                  > }
                  >
                  >[/color]


                  Comment

                  • mikeb

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

                    NoOne wrote:[color=blue]
                    > 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".
                    >[/color]

                    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.
                    [color=blue]
                    >
                    > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                    > news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...
                    >[color=green]
                    >>// 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.WriteLi ne("flag one is set");
                    >> }
                    >>
                    >> if ( (f & SomeFlags.Two) > 0)
                    >> {
                    >> Console.WriteLi ne("flag two is set");
                    >> }
                    >>
                    >> if ( (f & SomeFlags.Three ) > 0)
                    >> {
                    >> Console.WriteLi ne("flag three is set");
                    >> }
                    >>
                    >> if ( (f & SomeFlags.Four) > 0)
                    >> {
                    >> Console.WriteLi ne("flag four is set");
                    >> }
                    >>
                    >> if ( (f & SomeFlags.Five) > 0)
                    >> {
                    >> Console.WriteLi ne("flag five is set");
                    >> }
                    >>
                    >> if ( (f & SomeFlags.Six) > 0)
                    >> {
                    >> Console.WriteLi ne("flag six is set");
                    >> }
                    >> Console.ReadLin e();
                    >> }
                    >> }
                    >>}
                    >>
                    >>[/color]
                    >
                    >
                    >[/color]

                    --
                    mikeb

                    Comment

                    • 100

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

                      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_WINDOWEDG E = 0x00000100L,
                      WS_EX_CLIENTEDG E = 0x00000200L,
                      WS_EX_OVERLAPPE DWINDOW = 0x00000300
                      }

                      WS_EX_OVERLAPPE DWINDOW (WS_EX_WINDOWED GE | WS_EX_CLIENTEDG E) = 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


                      "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      news:uUN6Q9mkDH A.2676@TK2MSFTN GP11.phx.gbl...[color=blue]
                      > What i meant was bloody obvious and expected as its a FLAG, why the hell
                      > would I preform base 10 operations on a base 2 type (Flag)?
                      > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      > news:uPpMV8mkDH A.1740@TK2MSFTN GP12.phx.gbl...[color=green]
                      > > It should be powers of 2 by default for flags.
                      > >
                      > > Thats my point.
                      > >
                      > >
                      > > "Alan Pretre" <no@spam> wrote in message
                      > > news:#sbsc6mkDH A.4008@TK2MSFTN GP11.phx.gbl...[color=darkred]
                      > > > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                      > > > news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...
                      > > > > // Run this code and look for his obvious error by the compiler
                      > > >
                      > > > What you wrote and what you meant are two different things. The[/color][/color]
                      > compiler[color=green]
                      > > is[color=darkred]
                      > > > fine.
                      > > >
                      > > > You have:
                      > > > enum SomeFlags
                      > > > {
                      > > > None = 0, // 0000
                      > > > One, // 0001
                      > > > Two, // 0010
                      > > > Three, // 0011
                      > > > Four, // 0100
                      > > > Five, // 0101
                      > > > Six, // 0110
                      > > > All = One | Two | Three | Four | Five | Six // 0111
                      > > > };
                      > > >
                      > > > You meant:
                      > > > enum SomeFlags
                      > > > {
                      > > > None = 0,
                      > > > One = 1,
                      > > > Two = 2,
                      > > > Three = 4,
                      > > > Four = 8,
                      > > > Five = 16,
                      > > > Six = 32,
                      > > > All = One | Two | Three | Four | Five | Six
                      > > > }
                      > > >
                      > > > Change your code and it works fine.
                      > > >
                      > > > -- Alan
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • Fabian Schmied

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

                        > However, I think that his name-calling approach of attempting to get his[color=blue]
                        > point across is certainly counter productive.[/color]

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


                        Comment

                        • Alan Pretre

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

                          "100" <100@100.com> wrote in message
                          news:uhQjHknkDH A.1656@tk2msftn gp13.phx.gbl...[color=blue]
                          > Some of the flags might have more the one bit set to 1 and thay might be[/color]
                          not[color=blue]
                          > power of 2. This doesn't makes them *less flags* than the others, though.[/color]

                          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


                          Comment

                          • Alan Pretre

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

                            "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                            news:%23OzLADnk DHA.2772@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                            > 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.[/color]

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

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


                            Comment

                            • Bruno Jouhier [MVP]

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

                              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.

                              "Mr.Tickle" <MrTickle@mrmen .com> a écrit dans le message de
                              news:eeFm2tmkDH A.3688@TK2MSFTN GP11.phx.gbl...[color=blue]
                              > // 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.WriteLi ne("flag one is set");
                              > }
                              >
                              > if ( (f & SomeFlags.Two) > 0)
                              > {
                              > Console.WriteLi ne("flag two is set");
                              > }
                              >
                              > if ( (f & SomeFlags.Three ) > 0)
                              > {
                              > Console.WriteLi ne("flag three is set");
                              > }
                              >
                              > if ( (f & SomeFlags.Four) > 0)
                              > {
                              > Console.WriteLi ne("flag four is set");
                              > }
                              >
                              > if ( (f & SomeFlags.Five) > 0)
                              > {
                              > Console.WriteLi ne("flag five is set");
                              > }
                              >
                              > if ( (f & SomeFlags.Six) > 0)
                              > {
                              > Console.WriteLi ne("flag six is set");
                              > }
                              > Console.ReadLin e();
                              > }
                              > }
                              > }
                              >
                              >[/color]


                              Comment

                              • Mr.Tickle

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

                                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" <no@spam> wrote in message
                                news:#3Ew2dpkDH A.1948@TK2MSFTN GP12.phx.gbl...[color=blue]
                                > "Mr.Tickle" <MrTickle@mrmen .com> wrote in message
                                > news:%23OzLADnk DHA.2772@TK2MSF TNGP10.phx.gbl. ..[color=green]
                                > > 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.[/color]
                                >
                                > Using what you propose, what would be the value of b here?
                                >
                                > [Flags]
                                > enum Flags {
                                > a = 6,
                                > b
                                > }
                                >
                                >[/color]


                                Comment

                                Working...