A const idiom + a different 'delete'
bearophile
bearophileHUGS at lycos.com
Wed Aug 11 12:25:41 PDT 2010
Jonathan M Davis:
> I think that's a total no-go because it would depend on program flow. You're
> trying to alter a compile-time property at runtime. All it takes is having
> delete within an if statement, and all of a sudden, depending on how your
> program runs, the name may or may not be visible.
I don't agree. To test a runtime value you need an if statement, and it creates the scope of its then and else parts, if you remove a variable inside one of those two scopes, x is present again when the then and else scopes are finished. So what's the problem?
import std.conv: to;
void main(string[] args) {
int x = 10;
enum int y = 5;
// here x can be used
if (args.length > 1) {
// here x can be used
delete x;
// here x can't be used
float x; // not allowed
} else {
// here x can be used
}
// here x can be used
static if (y < 2)
delete x;
// here x can or can't be used according to the compile-time value of y
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list