www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static with?

reply "bearophile" <bearophileHUGS lycos.com> writes:
I think I'd like with() to not create a scope, just like the 
"static if". So you could write this code:


enum Foo { A, B }
auto x1 = Foo.A;
auto x2 = Foo.B;
void main() {
     import std.stdio;
     immutable data = [Foo.A, Foo.B];
     data.writeln;
}



Like this, where "with(Foo)" is usable at module scope, and it 
can be used to define an immutable and keep it visible later for 
the writeln:


enum Foo { A, B }
with (Foo) {
     auto x1 = A;
     auto x2 = B;
}
void main() {
     import std.stdio;
     with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
}


Assuming the current design of with() can't change, I think a 
"static with" could be used...


enum Foo { A, B }
static with (Foo) {
     auto x1 = A;
     auto x2 = B;
}
void main() {
     import std.stdio;
     static with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
}


Bye,
bearophile
Dec 14 2013
next sibling parent "JR" <zorael gmail.com> writes:
On Sunday, 15 December 2013 at 02:03:09 UTC, bearophile wrote:
 I think I'd like with() to not create a scope, just like the 
 "static if". So you could write this code:
 [...]
And/or with the same syntax as <attribute>: (saving some indentation, but perhaps less idiomatic?). Granted it doesn't allow for top-level definitions, and it *really* would highlight the need for a way to end the attribue scope. enum Foo { A, B } void main() { import std.stdio; with (Foo): immutable data = [A, B]; // ~with (Foo) ? pretty pretty please, would help so very much elsewhere data.writeln(); } In my code I usually locally alias the enum to a single letter and use it as a shorthand identifier. But it's ugly. enum DescriptiveEnumNameBecauseReasons { A, B } void main() { import std.stdio; alias d = DescriptiveEnumNameBecauseReasons; immutable data = [d.A, d.B]; data.writeln(); }
Dec 15 2013
prev sibling parent reply "Idan Arye" <GenericNPC gmail.com> writes:
On Sunday, 15 December 2013 at 02:03:09 UTC, bearophile wrote:
 I think I'd like with() to not create a scope, just like the 
 "static if". So you could write this code:


 enum Foo { A, B }
 auto x1 = Foo.A;
 auto x2 = Foo.B;
 void main() {
     import std.stdio;
     immutable data = [Foo.A, Foo.B];
     data.writeln;
 }



 Like this, where "with(Foo)" is usable at module scope, and it 
 can be used to define an immutable and keep it visible later 
 for the writeln:


 enum Foo { A, B }
 with (Foo) {
     auto x1 = A;
     auto x2 = B;
 }
 void main() {
     import std.stdio;
     with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
 }


 Assuming the current design of with() can't change, I think a 
 "static with" could be used...


 enum Foo { A, B }
 static with (Foo) {
     auto x1 = A;
     auto x2 = B;
 }
 void main() {
     import std.stdio;
     static with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
 }


 Bye,
 bearophile
I don't think `static` would be a good syntax here. `static` in D means either "belongs to the global\thread lifetime instead of the object/function lifetime" or "performed at compile-time". It's bad enough we have two meanings for the same keyword, but at least `static` is a good description for both of them. `static` does not describe well the idea of "not creating a new scope", and I don't think it's a good idea to overload another meaning on it and make it a whore keyword...
Dec 15 2013
parent reply "Orvid King" <blah38621 gmail.com> writes:
On Sun, 15 Dec 2013 09:02:12 -0600, Idan Arye <GenericNPC gmail.com> wrote:

 On Sunday, 15 December 2013 at 02:03:09 UTC, bearophile wrote:
 I think I'd like with() to not create a scope, just like the "static  
 if". So you could write this code:


 enum Foo { A, B }
 auto x1 = Foo.A;
 auto x2 = Foo.B;
 void main() {
     import std.stdio;
     immutable data = [Foo.A, Foo.B];
     data.writeln;
 }



 Like this, where "with(Foo)" is usable at module scope, and it can be  
 used to define an immutable and keep it visible later for the writeln:


 enum Foo { A, B }
 with (Foo) {
     auto x1 = A;
     auto x2 = B;
 }
 void main() {
     import std.stdio;
     with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
 }


 Assuming the current design of with() can't change, I think a "static  
 with" could be used...


 enum Foo { A, B }
 static with (Foo) {
     auto x1 = A;
     auto x2 = B;
 }
 void main() {
     import std.stdio;
     static with (Foo) {
         immutable data = [A, B];
     }
     data.writeln;
 }


 Bye,
 bearophile
I don't think `static` would be a good syntax here. `static` in D means either "belongs to the global\thread lifetime instead of the object/function lifetime" or "performed at compile-time". It's bad enough we have two meanings for the same keyword, but at least `static` is a good description for both of them. `static` does not describe well the idea of "not creating a new scope", and I don't think it's a good idea to overload another meaning on it and make it a whore keyword...
Your missing it's meaning when used on imports, which requires you to use the full module name to access it's members. On the topic of static with, perhaps modifying with to accept a type as it's argument would be enough?
Dec 15 2013
parent "Idan Arye" <GenericNPC gmail.com> writes:
On Sunday, 15 December 2013 at 17:30:03 UTC, Orvid King wrote:
 Your missing it's meaning when used on imports, which requires 
 you to use the full module name to access it's members.
Good point - I guess it already is a whore keyword...
 On the topic of static with, perhaps modifying with to accept a 
 type as it's argument would be enough?
Care to elaborate? I don't see how `with(<type>)` can supply the original functionality of `with`, let alone solve the scope problem...
Dec 15 2013