Does anyone use 'with' statement?

Xinok xnknet at gmail.com
Sun Dec 9 02:55:16 PST 2007


Bill Baxter wrote:
> Xinok wrote:
>> I can see where with statements can make code harder to read. I think 
>> it's important to make it more explicit, so others reading your code 
>> don't have to guess where a symbol is coming from.
>>
>> We could overload the global operator to do just that. It's purpose 
>> changes when it's used in the with statement.
>>
>> class N{
>>     int a, b, c;
>> }
>>
>> void main(){
>>     int a, b, c;
>>     N obj = new n;
>>     with(obj){
>>         a = 35; // main.a
>>         .a = 60; // obj.a
>>     }
>> }
> 
> I'd like to see it be a shadowing error if you try to use 'a' inside 
> with(obj) when 'a' is both a member of obj and the enclosing scope, as 
> above.
> 
> And I think '.a' is already used elsewhere to mean the 'a' from the 
> _outer_ scope.  Maybe I'm imagining that though... I've never actually 
> used it, but I was thinking I read that was D's version of ::a from c++.
> 
> Note that if you need to differentiate you can also still refer 
> explicitly to "obj.a" inside the with(obj) block.  "obj" doesn't cease 
> to exist as a symbol.
> 
> --bb

Yes, '.a' is, I think it's called, the global operator. I was suggesting 
that we could overload this operator and change it's meaning inside of 
with statments.

What you suggested, "shadowing error", could help avoid a few bugs. 
However, I don't think it will make the code any easier to read. The 
problem still exists that you may not know where a symbol is coming from.



More information about the Digitalmars-d mailing list