Re: Is flags enum needed in Phobos?

2012-10-15 Thread bearophile

Era Scarecrow:

But more likely it will be merged with another file, likely 
std.bitmanip or something...


A Phobos pull request is OK even if it's meant to be merged with 
std.bitmanip. Pull requests are not just for separate modules.


On GitHub people will be able to comment on single lines of code, 
etc.


Bye,
bearophile


Re: Is flags enum needed in Phobos?

2012-10-14 Thread Era Scarecrow

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


Re: Is flags enum needed in Phobos?

2012-10-14 Thread Era Scarecrow

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.


Re: Is flags enum needed in Phobos?

2012-10-14 Thread bearophile

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


Re: Is flags enum needed in Phobos?

2012-10-14 Thread Era Scarecrow

On Monday, 15 October 2012 at 02:54:38 UTC, bearophile wrote:

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?


 mmm... As I'm looking at it and making a reply, I've noticed a 
couple mistakes. Give me an hour or so to test and add it in. If 
all's well it will be fixed/added



Are you going to create a Phobos GitHugHub pull request?


 Wasn't planning on it. If you feel the library is good enough, I 
may add a request. But more likely it will be merged with another 
file, likely std.bitmanip or something...


Re: Is flags enum needed in Phobos?

2012-10-14 Thread Era Scarecrow
 K I have opOpAssign working. I may have to clean up 
documentation a little and clean the unittests up a bit, but it 
all appears to be working.


 Feel free to give it a try.

 I have added opSlice, which returns an array of your enum of all 
the flags it qualifies for. An empty enum (say, zero) never 
qualifies as it has no bits to make it unique (or it would always 
be present no matter what).


 Mmm... I actually hope that's all that's needed for it.


Re: Is flags enum needed in Phobos?

2012-10-07 Thread Denis Shelomovskij

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


Re: Is flags enum needed in Phobos?

2012-10-07 Thread Era Scarecrow
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).


Re: Is flags enum needed in Phobos?

2012-10-06 Thread Denis Shelomovskij

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


Re: Is flags enum needed in Phobos?

2012-10-06 Thread 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.


Re: Is flags enum needed in Phobos?

2012-09-26 Thread Denis Shelomovskij

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?





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?

--
Денис В. Шеломовский
Denis V. Shelomovskij


Re: Is flags enum needed in Phobos?

2012-09-26 Thread Dmitry Olshansky

On 26-Sep-12 21:39, Denis Shelomovskij wrote:

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?


I'd suggest an explicit way to do so. Like to!(flagEnum) or a standalone 
helpers.



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

If to! is used I guess throw exceptions if it doesn't fit.


* Is `flagEnum` an appropriate name?


Since it's a type (right?) I'd think EnumFlag, FlagSet or even Java's 
EnumSet. Generally I think I like words 'set' and/or 'flag' in the name.



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?


First, calm down. Second, I, for one, like it.
The place to go is most likely std.bitmanip or std.typecons. I haven't 
looked much at the actual code but the usage seems straightforward.


Another thought is to generalize it to something beyond 1-bit flags. How 
about packing various flags (set of 3-state flag, 4-state flag  etc.) in 
one fixed struct?



--
Dmitry Olshansky


Re: Is flags enum needed in Phobos?

2012-09-26 Thread Denis Shelomovskij

26.09.2012 21:52, Dmitry Olshansky пишет:

On 26-Sep-12 21:39, Denis Shelomovskij wrote:

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?


I'd suggest an explicit way to do so. Like to!(flagEnum) or a standalone
helpers.


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

If to! is used I guess throw exceptions if it doesn't fit.


* Is `flagEnum` an appropriate name?


Since it's a type (right?) I'd think EnumFlag, FlagSet or even Java's
EnumSet. Generally I think I like words 'set' and/or 'flag' in the name.


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?


First, calm down. Second, I, for one, like it.


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.



The place to go is most likely std.bitmanip or std.typecons. I haven't
looked much at the actual code but the usage seems straightforward.

Another thought is to generalize it to something beyond 1-bit flags. How
about packing various flags (set of 3-state flag, 4-state flag  etc.) in
one fixed struct?


Please give me usage examples (unittests), and I will implement the 
functionality. Now I don't understand what are n-state flags and what is 
the difference with std.bitmanip.bitfields.


--
Денис В. Шеломовский
Denis V. Shelomovskij


Re: Is flags enum needed in Phobos?

2012-09-26 Thread Dmitry Olshansky

On 26-Sep-12 22:17, Denis Shelomovskij wrote:


Please give me usage examples (unittests), and I will implement the
functionality. Now I don't understand what are n-state flags and what is
the difference with std.bitmanip.bitfields.



Well thinking more of it, bitfields indeed will do for a fixed bunch of 
flags (if it supports enums).


What's missing is an analog of BitArray but for arbitrary integer enum 
type to pack them efficiently (depending on number of possible values). 
Though I guess the usage is somewhat niche.



--
Dmitry Olshansky


Re: Is flags enum needed in Phobos?

2012-09-26 Thread bearophile

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


Re: Is flags enum needed in Phobos?

2012-09-26 Thread Michael
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





Re: Is flags enum needed in Phobos?

2012-09-26 Thread Jesse Phillips
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.


Re: Is flags enum needed in Phobos?

2012-09-25 Thread bearophile
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?


See also:
http://d.puremagic.com/issues/show_bug.cgi?id=6946

Bye,
bearophile


Re: Is flags enum needed in Phobos?

2012-09-25 Thread Denis Shelomovskij

25.09.2012 22:03, bearophile пишет:

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?


See also:
http://d.puremagic.com/issues/show_bug.cgi?id=6946

Bye,
bearophile


Thanks, I was almost sure there is such issue. I added to the issue a 
link to this thread.


--
Денис В. Шеломовский
Denis V. Shelomovskij


Re: Is flags enum needed in Phobos?

2012-09-25 Thread Faux Amis

On 25/09/2012 20:03, bearophile wrote:

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?


See also:
http://d.puremagic.com/issues/show_bug.cgi?id=6946

Bye,
bearophile


comment on code:
Isn't there a way to retrieve all reserved words?

And, did you do a pull request?


Re: Is flags enum needed in Phobos?

2012-09-25 Thread bearophile

Faux Amis:


Isn't there a way to retrieve all reserved words?


I think the answer is negative.



And, did you do a pull request?


I don't do those yet. And that code is just a prototype.

Bye,
bearophile


Re: Is flags enum needed in Phobos?

2012-09-25 Thread bearophile

Faux Amis:


Isn't there a way to retrieve all reserved words?


This is a possibility:

assert(__traits(is_reserved_word, import));

Bye,
bearophile