"with" still sucks + removing features + adding features

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 18 11:13:21 PDT 2009

Bill Baxter wrote:
> On Mon, May 18, 2009 at 10:46 AM, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>> Johan Granberg wrote:
>>> BCS wrote:
>>>> Hello Andrei,
>>>>> I think "with" is a very dangerous feature due to the way it hides
>>>>> symbols. It essentially makes the feeblest attempt at modular
>>>>> reasoning utterly impossible:
>>>>> int x, y;
>>>>> with (whatever)
>>>>> {
>>>>> y += x;
>>>>> ++x;
>>>>> }
>>>>> Maintenance of any type that is being used with "with" becomes a very
>>>>> dangerous proposition because it can silently change meaning of code.
>>>> I'd be willing to go the half way solution of making accessing a
>>>> shadowing
>>>> symbol an error, resulting in (loudly) not being able to access either.
>>> I think this solution is a good idea but that removing or restricting
>>> anything more regarding with is a bad idea as others have pointed out.
>>> I'm using with quit a lot and it was one of thous things that attracted me
>>> to D (I get the feeling that D has a quite pragmatic way of looking at
>>> language features, if it's usefull lets have it unless it's harmfull to
>>> other parts of D, sort of.).
>> I personally still think it's a bad feature because it introduces
>> long-distance coupling between symbols defined in two different places, both
>> distinct from the place where the statement is used! Consider:
>> import wyda;  // defines symbol write
>> import geeba; // defines struct S { ... }
>> void main()
>> {
>>   S s;
>>   with (s) {
>>      write(5);
>>   }
>> }
>> Machiavelly would jump in joy at such code. What did I achieve? I saved a
>> few "s.". What did I lose? The ability so say anything, but absolutely
>> anything on what the code does.
> Shouldn't the feature just follow the same rules as module imports do?
> If I say
>   import foo;
>   import bar;
>   void main() {
>       write(5);
>   }
> It's fine as long as foo and bar don't both define bar.  Why shouldn't
> the same scope resolution mechanism not apply to with?
> So in your example write(5) is fine as long as S and some module don't
> both define a write().
> I think that's what others were saying in the other thread, too.
> Makes things more consistent too.   Given that do you still object to
> with() so vehemently?

I'd be happy if "with" were consistent in the way you describe. Nice idea!


More information about the Digitalmars-d mailing list