Can we have this Syntactic sugar.

Nick Sabalausky a at a.a
Tue Nov 24 08:34:58 PST 2009


"Robert Clipsham" <robert at octarineparrot.com> wrote in message 
news:hegv1c$1mo6$1 at digitalmars.com...
> Long Chang wrote:
>> class RegExp
>> {
>>     enum Option{
>>         X, Y, Z
>>     }
>>     int options;
>>     this(int options = 0){ this.options = options; }
>> }
>>
>> void main(){
>>        auto reg  = new RegExp("^A.",   .Option(  X |Y|Z ) );
>>        assert( RegExp .Option.X | RegExp .Option.Y| RegExp .Option.Z   == 
>> reg.options );
>> }
>
> ----
> class Foo
> {
>         enum Option
>         {
>                 X = 2,
>                 Y = 4,
>                 Z = 8
>         }
>         int options;
>         this(int opts = 0)
>         {
>                 options = opts;
>         }
> }
>
> void main()
> {
>         Foo foo;
>         with( foo.Option )
>         {
>                 foo = new Foo( X | Y | Z );
>                 assert( X | Y | Z == foo.options  );
>         }
> }
> ----
>
> Is this the kind of thing you're looking for? That looks nicer than your 
> proposed syntax in my opinion :)

Maybe what would be good is an expression version of with:

foo = new Foo( with(Foo.Option)( X | Y | Z ) );
assert( with(Foo.Option)( X | Y | Z ) == foo.options  );

Although I think I still prefer something closer to Long Chang's version, 
which is maybe less general but easier to read because it's less verbose and 
less paren-happy:

foo = new Foo( Foo.Option( X | Y | Z ) );
assert( Foo.Option( X | Y | Z ) == foo.options  );

Come to think of it though, I think my favorite is still making the 
"Foo.Option." optional wherever a Foo.Option is expected. But, I'd consider 
anything (except the Haxe-style approach of polluting the namespace with all 
of the unqualified enum values - I *hate* when languages do that) to be a 
very welcome improvement.





More information about the Digitalmars-d mailing list