On Sun, 15 Dec 2013 09:02:12 -0600, Idan Arye <[email protected]> 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?