View mode: basic / threaded / horizontal-split · Log in · Help
September 26, 2012
Re: Is flags enum needed in Phobos?
Denis Shelomovskij:

> OK. Looks like such functionality isn't needed and I don't have 
> to do a pull request. What about to close
> http://d.puremagic.com/issues/show_bug.cgi?id=6946
> with WONTFIX?

I think it's a good idea to have a well written EnumFlags data 
structure in Phobos. In C# this feature is even built-in. So 
issue 6946 is not closing, unless Andrei or Walter decide it's a 
bad idea to have something like this in Phobos.

Bye,
bearophile
September 26, 2012
Re: Is flags enum needed in Phobos?
On Tuesday, 25 September 2012 at 17:13:44 UTC, Denis Shelomovskij 
wrote:
> .NET has FlagsAttribute, Java has EnumSet. Looks like we need 
> this too. How about to add a library solution to Phobos?
>
+1.


> Also I'm not sure:
> * Should we support converting from a number to a flag enum?
+1.

> * If so, should we support values not from enum flags or throw 
> exceptions (it may be configurable)?

http://msdn.microsoft.com/en-us/library/system.enum.aspx
IsDefined method.

> * Is `flagEnum` an appropriate name?
Just "enum" with additional methods like: HasFlag, IsFlag
September 26, 2012
Re: Is flags enum needed in Phobos?
On Wednesday, 26 September 2012 at 18:16:54 UTC, Denis 
Shelomovskij wrote:
> Thanks for the answer, but still there are only few guys here 
> interesting in it to be included in Phobos, so WONTFIX-ing the 
> issue looks reasonable. I can just support this struct in my 
> library for you.

There are few things you will see a huge response to. Language 
changes are harder to get in, library additions pretty easy. If 
you want to create a library solution for Phobos, do it. When it 
is done either it will get included or will be usable as a third 
party library.

I for one am not against the inclusion, and think it may be a 
good addition but also am not versed enough to make a claim for 
it. Namely I only just saw this and haven't looked at if it 
solves some problem I'd have.
October 06, 2012
Re: Is flags enum needed in Phobos?
25.09.2012 21:14, Denis Shelomovskij пишет:
> .NET has FlagsAttribute, Java has EnumSet. Looks like we need this too.
> How about to add a library solution to Phobos?
>
> My variant is here (search for `flagEnum`):
> https://bitbucket.org/denis_sh/misc/src/tip/stdd/typecons.d
>
> It has a bug and I have no idea how to fix it:
> `AB.init |= AB.a` and `AB.a |= AB.a` are allowed.
> (no, we can't make `AB.a` const to disallow the second case because it
> will disallow this: `auto a = AB.a; a |= AB.a;`)
>
> Also I'm not sure:
> * Should we support converting from a number to a flag enum?
> * If so, should we support values not from enum flags or throw
> exceptions (it may be configurable)?
> * Is `flagEnum` an appropriate name?
>
>
>

Just for those who are interested in my `flagEnum` implementation:

It is in https://github.com/denis-sh/phobos-additions/ project now.

Docs (with links to source) here:
http://denis-sh.github.com/phobos-additions/unstd.typecons.html

-- 
Денис В. Шеломовский
Denis V. Shelomovskij
October 06, 2012
Re: Is flags enum needed in Phobos?
On Wednesday, 26 September 2012 at 19:11:37 UTC, bearophile wrote:
> I think it's a good idea to have a well written EnumFlags data 
> structure in Phobos. In C# this feature is even built-in. So 
> issue 6946 is not closing, unless Andrei or Walter decide it's 
> a bad idea to have something like this in Phobos.

 I've written one I believe if sufficient and am using in my 
current project. I'll add more to my branch as time goes on.

https://github.com/rtcvb32/Side-Projects

 example in documentation

[code]
  enum ETEST {none = 0, one = 1, two = 2, three = 3, four = 4}
  handle_flags!(ETEST, int) ftest;

  ftest.set_flag(ETEST.two);	//check flag setting
  assert(ftest.state == 2);	//int and enum compares.
  assert(ftest.state == ETEST.two);

  ftest.set_flag(ETEST.four);	//set second flag
  assert(ftest.state == 6);	//2+4, no 6 enum.

  //Flags can also encompass multiple bits.
  //in this case all bits returned with an
  //AND must match the flag.
  ftest.state = 1;	
  assert(!ftest.checkAll(one, two, three));  //can't be true, 
only 1 is there
  ftest.state = 3;
  assert(ftest.checkAll(one, two, three));   //must be true, 
since 1+2 includes 3.
[/code]

 Feel free to scrutinize and offer suggestions.

 Although it doesn't appear to be mentioned, you can also 
shorthand the flag with your alias. So ftest.one would be the 
same as ETEST.one or ftest.Enum.one. I recommend using with when 
you are using such flags.
October 07, 2012
Re: Is flags enum needed in Phobos?
06.10.2012 23:50, Era Scarecrow пишет:
> On Wednesday, 26 September 2012 at 19:11:37 UTC, bearophile wrote:
>> I think it's a good idea to have a well written EnumFlags data
>> structure in Phobos. In C# this feature is even built-in. So issue
>> 6946 is not closing, unless Andrei or Walter decide it's a bad idea to
>> have something like this in Phobos.
>
>   I've written one I believe if sufficient and am using in my current
> project. I'll add more to my branch as time goes on.
>
> https://github.com/rtcvb32/Side-Projects
>
>   example in documentation
>
> [code]
>    enum ETEST {none = 0, one = 1, two = 2, three = 3, four = 4}
>    handle_flags!(ETEST, int) ftest;
>
>    ftest.set_flag(ETEST.two);    //check flag setting
>    assert(ftest.state == 2);    //int and enum compares.
>    assert(ftest.state == ETEST.two);
>
>    ftest.set_flag(ETEST.four);    //set second flag
>    assert(ftest.state == 6);    //2+4, no 6 enum.
>
>    //Flags can also encompass multiple bits.
>    //in this case all bits returned with an
>    //AND must match the flag.
>    ftest.state = 1;
>    assert(!ftest.checkAll(one, two, three));  //can't be true, only 1 is
> there
>    ftest.state = 3;
>    assert(ftest.checkAll(one, two, three));   //must be true, since 1+2
> includes 3.
> [/code]
>
>   Feel free to scrutinize and offer suggestions.
>
>   Although it doesn't appear to be mentioned, you can also shorthand the
> flag with your alias. So ftest.one would be the same as ETEST.one or
> ftest.Enum.one. I recommend using with when you are using such flags.

I'd like to see enum syntax for flug enum. So I dislike function calls 
like `set_flag`, `checkAll`, etc. (IMHO)
-- 
Денис В. Шеломовский
Denis V. Shelomovskij
October 07, 2012
Re: Is flags enum needed in Phobos?
On Sunday, 7 October 2012 at 09:22:17 UTC, Denis Shelomovskij 
wrote:
> I'd like to see enum syntax for flug enum. So I dislike  
> function calls like `set_flag`, `checkAll`, etc. (IMHO)

 You mean... binary basic syntax? That shouldn't be too hard. So 
something like..?

 Flags x;
 with(Flags) {
  x += one; //setflag
  x -= two; //clearflag
  x ^= four;//flipflag
  x += [one,two]; //set flags multiple, same with flipping and 
clearing
  x -= [one,two];
  x ^= [one,two];

//append ~ seemingly would be unused, or equal to +

  x = one; //reset completely to flag 'one'
  assert(x[one]);  //checking, feels like an AA
  assert(!x[two]);
  x[two] = true; //optionally secondary way to set flag
  assert(x[one, two]); //only true if both are set.

  Flags y, z;

  z = x & y; //keep only flags within y
  z = x | y; //copy flags of both x & y
  z = x ^ y; //flip flags in x from y
 }

 This wouldn't be hard to add, but I was going for basic 
functionality, the extras are easy to use/add afterwards :)

 Probably for individual flags I'd use a template so when you 
optimize it will give you the smallest executing size for your 
results. Hmmm...

 The else is the only one I'm seeing a problem implementing 
easily, although using the built in types you can do it yourself. 
I'm not going to add odd binary operators if it doesn't make 
sense (Like shifting appending or slicing).
October 14, 2012
Re: Is flags enum needed in Phobos?
On Sunday, 7 October 2012 at 09:48:50 UTC, Era Scarecrow wrote:
> On Sunday, 7 October 2012 at 09:22:17 UTC, Denis Shelomovskij 
> wrote:
>> I'd like to see enum syntax for flug enum. So I dislike 
>> function calls like `set_flag`, `checkAll`, etc. (IMHO)
>
>  You mean... binary basic syntax? That shouldn't be too hard. 
> So something like..?
>
>  Flags x;
>  with(Flags) {
>   x += one; //setflag
>   x -= two; //clearflag
>   x ^= four;//flipflag
>   x += [one,two]; //set flags multiple, same with flipping and 
> clearing
>   x -= [one,two];
>   x ^= [one,two];


 I've added binary functionality. Looking at these notes I see I 
forgot the enum/array part; And checking for binary true isn't 
added yet. However I'll get to it sooner or later. Both opBinary 
and opOpBinary are supported so far.

>   x += one; //setflag
>   x -= two; //clearflag
>   x ^= four;//flipflag
    x &= two; //keeponly
    x |= two; //setFlag, same as +=

    //should work fine, as with all operations, const/immutable 
safe.
    Flags y = x + one;

 if (x){} //compile error, will fix later
 if (x != Flags()){} //should work, compare against .init 
(probably 0)
 if (x.state){}      //works but you shouldn't depend on this
October 15, 2012
Re: Is flags enum needed in Phobos?
On Sunday, 14 October 2012 at 06:34:39 UTC, Era Scarecrow wrote:
> I've added binary functionality. Looking at these notes I see I 
> forgot the enum/array part; And checking for binary true isn't 
> added yet. However I'll get to it sooner or later. Both 
> opBinary and opOpBinary are supported so far.

 I can't get opOpAssign working properly, and it's worse with 
arrays. Removing opOpAssign, everything else (arrays, single 
flags, other flags) works properly. It's likely the last time 
I'll update it for a while.

 And I guess that's it.
October 15, 2012
Re: Is flags enum needed in Phobos?
Era Scarecrow:

> I can't get opOpAssign working properly, and it's worse with 
> arrays. Removing opOpAssign, everything else (arrays, single 
> flags, other flags) works properly.

Are such problems known? In bugzilla or here? Are those fixable?


> It's likely the last time I'll update it for a while.
>
>  And I guess that's it.

Are you going to create a Phobos GitHugHub pull request?

Bye,
bearophile
1 2 3
Top | Discussion index | About this forum | D home