[Kde-bindings] Qyoto: new features

Arno Rehn arno at arnorehn.de
Tue Apr 10 17:32:01 UTC 2012

On 10/04/12 19:25, Richard Dale wrote:
> On Tuesday, April 10, 2012 01:10:32 AM Dimitar Dobrev wrote:
>> As far as I can see QFlags is used only on enums that support bitwise
>> operation, i.e. that are defined as 1, 2, 4, 8, etc. So it is impossible to
>> produce an invalid enum value using bitwise operations. As I said, QFlags
>> exists only because C++ accepts an integer where an enum value is required
>> thus violating type safety - a drawback C# does not have with its enums,
>> that is, calling MyMethod(MyEnum myEnum) with an integer (or a short, long,
>> etc.) fails to compile. So, if C++ enums were type-safe as C# enums, there
>> would be no QFlags because it'd serve no purpose. And then the one
>> difference between a working and a non-working flags enum would be that the
>> first one is correctly (1, 2, 4...) defined. Thank you for your opinion on
>> the matter, I appreciate it. I also appreciate your sample implementations
>> but I want the generated API to be, besides correct, as simple as possible.
> No, I don't think you are correct. C# has exactly the same problem as C++. If
> you AND or OR two enums together you don't get an enum type in either C++ or
> C#. For instance, from my example:
> public class FlagsTest
> {public enum MyEnum : uint {
>          ValA = 0x0,
>          ValB = 0x1,
>          ValC = 0x2,
>          ValD = 0x4
>      }
>      public void MyMethod(FlagsTest.MyEnum flags) {
>        ...
>      }
>      ...
> }
> If you call MyMethod(MyEnum.ValA&  MyEnum.ValB) you will get an error in C#.
> If you then define MyMethod() like this:
>     public void MyMethod(uint flags) {
>        ...
>      }
> You can call MyMethod(MyEnum.ValA&  MyEnum.ValB)  OK, but you can also call it
> with MyMethod(0x16) and it willl work fine. With my QFlags example class, the
> first call will work, but 0x16 will give a compile error.

I think Dimitar is right here. Enums in C# are specifically designed to 
be used in the same manner that QFlags in Qt is, and you should be able 
to pass ORed and ANDed values to a method that is expecting an enum.
Maybe the enum needs to be have the Flags attribute set for this to work:


As you can see, this really is the intended use case. Also, System.Enum 
has methods like HasFlag() which tests for a flag being set.

Arno Rehn

More information about the Kde-bindings mailing list