Flag proposal

Steven Schveighoffer schveiguy at yahoo.com
Sat Jun 11 07:30:24 PDT 2011


On Sat, 11 Jun 2011 09:25:19 -0400, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2011-06-11 09:05:52 -0400, "Steven Schveighoffer"  
> <schveiguy at yahoo.com> said:
>
>> As far as the negation, I think we need one more layer of type:
>>  struct FlagParam(string pname)
>> {
>>     Flag!pname value;
>>     alias value this;
>>     this(Flag!pname x) { this.value = x }
>>     FlagParam op???() const { return FlagParam(cast(Flag!pname)!value);  
>> }
>> }
>>  BTW, is there a way to hook "!"?  Maybe this won't work...  The idea  
>> is to  have Yes.abc return FlagParam!"abc"(Flag!"abc".yes).
>
> If you need to convert a bool to a Flag!"abc", you can do any of these:
>
> 	func(expression ? Yes.abc : No.abc);
>
> or
>
> 	func(cast(Flag!"abc")expression);
>
> or, with your proposal:
>
> 	func(FlagParam!"abc"(expression));
>
> None of this is very appealing, but I find the first is the most  
> readable.

We need another struct besides Yes and No that allows creation with  
another opDispatch:

Foo.abc(expression);

The best name for Foo is Flag, but that's taken...

But still, is there a way to override the type returned by !flag?  I think  
there isn't...  That's kind of sucky.  Flipping of a flag is a very common  
feature, I would expect to be able to do it easily.

Hm... what if opCast!bool() returns something other than bool?  does that  
actually work?  That *would* be a hack :)

We could also possibly use unary - to denote flipping a flag.  That's very  
hacky too, but I think it would work.

-Steve


More information about the Digitalmars-d mailing list