proposal: allow 'with(Foo):' in addition to 'with(Foo){..}'

Artur Skawina via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 11 05:25:40 PDT 2014


On 08/10/14 23:01, Era Scarecrow via Digitalmars-d wrote:
>  Does with have to be only for statements?
> 
>  Real example. In my code somewhere i have a large list of enum types that specify a type of formatting and visibility options.
[...]
>  Now since i can't use with(): I'm forced to do aliases, and a lot of them...
[...]
>  Had i been able to use with() i could have avoided the aliases above and probably just done a single line before using it heavily.
> 
>   with(FlagStates, ValueType):
> 
>  There wouldn't have been clashing because they each hold different types of data, and it's all immutable static values anyways.

What you're really looking for is context dependent access to the
target scope. That would work for statics and enums, but would
probably require a new syntax (it becomes too misleading and/or
ambiguous otherwise).

Right now, you can emulate declaration-with using hacks like:

   enum E1 { A, B, C }
   enum E2 { A, B, C, D, E, F }

   struct S { E1 e1; E2 e2; }

   immutable S[] d = {
      with (E2) with (E1) { mixin(q{
         immutable S[] r = [
            { E1.A, E2.D },
            { A, D },
            { B, E },
            { C, E2.C },
         ];
         return r;
   });}}();

Not pretty, but sometimes useful when you have a lot of such elements
and/or when they are generated from DSLs.

artur


More information about the Digitalmars-d mailing list