'with' bug?
Regan Heath
regan at netmail.co.nz
Fri Nov 2 05:03:29 PDT 2012
After reading this:
http://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/
I thought, does D have the same "problem", and according to:
http://dlang.org/statement.html#WithStatement
No, it doesn't. D detects local variable shadowing and produces an
error. But, then I thought does that apply to global variables as well?
Turns out, no it doesn't.
// [withd.d]
import std.stdio;
int global;
struct S
{
int global;
int local;
}
void main()
{
int local;
S s;
with(s)
{
local = 3; // withd.d(18): Error: with symbol withd.S.local is
shadowing local symbol withd.main.local
global = 5;
}
writefln("local = %d", local);
writefln("global = %d", global);
writefln("s.local = %d", s.local);
writefln("s.global = %d", s.global);
}
The above (if you comment out the line producing the expected error)
compiles and runs. It updates s.global at least. The risk is fairly
small, I guess, that someone will mis-type a member name, and hit a global
with that mis-typed name, but it's possible. And in that case it would
compile and then do something unexpected.
Should I raise a bug for this?
R
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
More information about the Digitalmars-d
mailing list