Puzzled by this behavior

Don Allen donaldcallen at gmail.com
Wed Jun 1 12:33:35 UTC 2022


On Wednesday, 1 June 2022 at 07:54:04 UTC, FeepingCreature wrote:
> On Tuesday, 31 May 2022 at 22:48:55 UTC, Don Allen wrote:
>> My concern is not "solving this". My concern is whether the 
>> language is clean and consistent so I can have mental model of 
>> it I can rely upon, rather than constantly searching through 
>> documentation to learn how a particular special case is 
>> handled. I also care a lot about writing readable code, and 
>> about being able to limit the visibility of variables to just 
>> the scope that needs them and no more. The workarounds you 
>> cite frankly feel like hacks to me, ugly ways of working 
>> around problems in the language design.
>>
>> /Don
>>
>>>
>>> -Steve
>
> IMO this is completely internally consistent. Your mental model 
> of a function should be a tree of scopes; your mental model of 
> a module should be a lazily resolved key-value store. They're 
> just fundamentally different things.
>
> You can make function scopes behave similar to global scopes, 
> ie. void delegate() foo; void bar() { foo(); } foo = { ... };, 
> but then you just move the compiletime error to runtime: ie. 
> void delegate() foo; foo(); You're basically manually 
> implementing dynamic scoping. But D is lexically scoped for 
> functions, not just with nested functions but with every 
> language element. *Modules* are the exception - or rather, 
> modules are just a fundamentally different thing.

I don't disagree with your description of D -- that module-level 
scopes are different than those at function level. You omitted 
struct-level scoping, which is similar to how modules are 
handled. And your discussion of how to make function-level 
scoping behave like module-level scoping ignores what I have 
previously written. I know of the availability of these 
workarounds and to me they are just band-aids on an odd language 
inconsistency.

I will say this once more and I'm done: function-level scoping in 
D is different from the other two D cases and different from 
other prominent lexically scoped languages. I question why that 
is. You assert that things are "internally" consistent. That may 
be so, but I would argue that that is completely irrelevant. What 
matters is *external* consistency, what the programmer sees.




More information about the Digitalmars-d mailing list