January 26, 2012
> It's not type safe in C. But you can wrap it in a struct with alias this instead.

Yep, but in D we have strong enums, so why not use them.
January 26, 2012
On 1/26/2012 8:55 PM, Michel Fortin wrote:
> On 2012-01-26 01:12:40 +0000, Jonathan M Davis <jmdavisProg@gmx.com> said:
>
>> On Thursday, January 26, 2012 02:06:45 Trass3r wrote:
>>> When writing C bindings I usually create lots of aliases via a
>>> string mixin to pull enum members into the enclosing scope so
>>> it's compatible to C.
>>> Would it be wise to let the compiler do this automatically for
>>> extern(C) enums?
>>
>> Why? You're using them in D code, not C code. What difference does it
>> make if
>> the enum is one that's used in C code or not? Why would you use such
>> aliases
>> with enums from C but not those from D/ What makes enums from C
>> different?
>
> Often C enum value naming takes into account that they'll live in the
> outer scope. For instance:
>
> enum UITableViewRowAnimation {
> UITableViewRowAnimationFade,
> UITableViewRowAnimationRight,
> UITableViewRowAnimationLeft,
> UITableViewRowAnimationTop,
> UITableViewRowAnimationBottom,
> UITableViewRowAnimationNone,
> UITableViewRowAnimationMiddle,
> UITableViewRowAnimationAutomatic = 100
> }
>
> So if you're doing direct bindings where you don't want to change the
> names, how do you use that in D?
>
> UITableViewRowAnimation.UITableViewRowAnimationFade
>
alias int UITableViewRowAnimation;
enum
{
UITableViewRowAnimationFade,
UITableViewRowAnimationRight,
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone,
UITableViewRowAnimationMiddle,
UITableViewRowAnimationAutomatic = 100
}
January 26, 2012
On 2012-01-26 14:23, Trass3r wrote:
>> It's not type safe in C. But you can wrap it in a struct with alias
>> this instead.
>
> Yep, but in D we have strong enums, so why not use them.

What about be able to do something like this:

enum Foo
{
public:
    bar,
    fooBar,
}

Foo f = bar;

-- 
/Jacob Carlborg
January 26, 2012
On 1/26/2012 10:23 PM, Trass3r wrote:
>> It's not type safe in C. But you can wrap it in a struct with alias
>> this instead.
>
> Yep, but in D we have strong enums, so why not use them.

If your binding is for yourself, that's not a big deal. But if you're putting it out there for public consumption, then I think compatibility with the C version would be more important. If someone is looking at sample C code, you should make it they don't need to adjust it much at all. In some cases, this is unavoidable (bit fields, macros), but where it *is* avoidable, it should be.
January 26, 2012
That would break the independence between parser and semantic analyzer, because there's no way to disambiguate "bar" from "Foo.bar" without knowing, that "Foo" is actually an enum.

On Thu, Jan 26, 2012 at 5:41 PM, Jacob Carlborg <doob@me.com> wrote:
> On 2012-01-26 14:23, Trass3r wrote:
>>>
>>> It's not type safe in C. But you can wrap it in a struct with alias this instead.
>>
>>
>> Yep, but in D we have strong enums, so why not use them.
>
>
> What about be able to do something like this:
>
> enum Foo
> {
> public:
>    bar,
>    fooBar,
> }
>
> Foo f = bar;
>
> --
> /Jacob Carlborg



-- 
Bye,
Gor Gyolchanyan.
January 26, 2012
On 01/26/2012 07:21 PM, Gor Gyolchanyan wrote:
> That would break the independence between parser and semantic
> analyzer, because there's no way to disambiguate "bar" from "Foo.bar"
> without knowing, that "Foo" is actually an enum.
>

No, it would not. The parser does not have to care.

> On Thu, Jan 26, 2012 at 5:41 PM, Jacob Carlborg<doob@me.com>  wrote:
>> On 2012-01-26 14:23, Trass3r wrote:
>>>>
>>>> It's not type safe in C. But you can wrap it in a struct with alias
>>>> this instead.
>>>
>>>
>>> Yep, but in D we have strong enums, so why not use them.
>>
>>
>> What about be able to do something like this:
>>
>> enum Foo
>> {
>> public:
>>     bar,
>>     fooBar,
>> }
>>
>> Foo f = bar;
>>
>> --
>> /Jacob Carlborg
>
>
>

January 26, 2012
On 01/26/2012 02:41 PM, Jacob Carlborg wrote:
> On 2012-01-26 14:23, Trass3r wrote:
>>> It's not type safe in C. But you can wrap it in a struct with alias
>>> this instead.
>>
>> Yep, but in D we have strong enums, so why not use them.
>
> What about be able to do something like this:
>
> enum Foo
> {
> public:
> bar,
> fooBar,
> }
>
> Foo f = bar;
>

public is the wrong keyword. Furthermore, the solution is not better than mixin Import!Foo; I think the extern(C) enum proposal is pragmatic and makes more sense.
January 26, 2012
> If your binding is for yourself, that's not a big deal. But if you're putting it out there for public consumption, then I think compatibility with the C version would be more important. If someone is looking at sample C code, you should make it they don't need to adjust it much

Yep, one big argument for my proposal.
January 26, 2012
>> What about be able to do something like this:
>>
>> enum Foo
>> {
>> public:
>> bar,
>> fooBar,
>> }
>>
>> Foo f = bar;
>>
>
> public is the wrong keyword. Furthermore, the solution is not better than mixin Import!Foo; I think the extern(C) enum proposal is pragmatic and makes more sense.

+1
January 27, 2012
> alias int UITableViewRowAnimation;
> enum
> {
> UITableViewRowAnimationFade,
> UITableViewRowAnimationRight,
> UITableViewRowAnimationLeft,
> UITableViewRowAnimationTop,
> UITableViewRowAnimationBottom,
> UITableViewRowAnimationNone,
> UITableViewRowAnimationMiddle,
> UITableViewRowAnimationAutomatic = 100
> }

That works for interfacing c, but not c++.

The following is a better solution, and should probably be in the standard library.

enum UITableViewRowAnimation
{
  UITableViewRowAnimationFade,
  UITableViewRowAnimationRight,
  UITableViewRowAnimationLeft,
  UITableViewRowAnimationTop,
  UITableViewRowAnimationBottom,
  UITableViewRowAnimationNone,
  UITableViewRowAnimationMiddle,
  UITableViewRowAnimationAutomatic = 100
}
alias UITableViewRowAnimation.UITableViewRowAnimationFade
UITableViewRowAnimationFade;
alias UITableViewRowAnimation.UITableViewRowAnimationRight
UITableViewRowAnimationRight;
alias UITableViewRowAnimation.UITableViewRowAnimationLeft
UITableViewRowAnimationLeft;
alias UITableViewRowAnimation.UITableViewRowAnimationTop
UITableViewRowAnimationTop;
alias UITableViewRowAnimation.UITableViewRowAnimationBottom
UITableViewRowAnimationBottom;
alias UITableViewRowAnimation.UITableViewRowAnimationNo
UITableViewRowAnimationNo;
alias UITableViewRowAnimation.UITableViewRowAnimationMiddle
UITableViewRowAnimationMiddle;
alias UITableViewRowAnimation.UITableViewRowAnimationAutomatic
UITableViewRowAnimationAutomatic;

(could be mixin(exposeEnumMembers!UITableViewRowAnimation); )