with and shadowing variables
Jim Balter
Jim at Balter.name
Mon Jul 23 12:15:28 UTC 2018
On Sunday, 22 July 2018 at 14:05:45 UTC, Jonathan M Davis wrote:
> On Sunday, July 22, 2018 12:13:43 Anonymouse via Digitalmars-d
> wrote:
>> Can this be made a compiler warning?
>>
>> struct Foo
>> {
>> int i;
>> }
>>
>> void main()
>> {
>> Foo foo;
>>
>> with (foo)
>> {
>> i = 42;
>> int i;
>> i = 24;
>> }
>> }
>>
>> I'm hesitant to file a bug because it'll just be immediately
>> closed with a link to
>> https://dlang.org/spec/statement.html#WithStatement. I
>> understand that's how it works, but it's weird and weak to
>> human mistakes.
>
> Given the shadowing protections listed in #5, it could
> certainly be argued that it would be in the spirit of the
> restrictions that with already has, and I think that there's a
> pretty clear argument to be made that allowing it is too
> error-prone, but maybe someone will have a reason why it
> doesn't make sense to disallow it. I don't know. Regardless, I
> would suggest that you open an enhancement request. I would
> guess that it's straightforward enough that a DIP isn't
> reauired so long as Walter approves of it, but I don't know.
> Either way, if it's in bugzilla, then it stands a much better
> chance of happening than if the only record of it is here.
>
> - Jonathan M Davis
#5 says that a symbol defined in an outer scope and used within
the with block must not also be a member of Foo, to prevent a
silent change in the meaning of the code if that symbol gets
added to Foo -- #5 will result in an error message if that
happens. Nothing like that applies here ... clearly the two `i's
are different, since you can't use a symbol before it's defined
(except at top level). You might want to argue that it should be
disallowed (I wouldn't), but I don't think you can use "the
spirit of #5" to do so.
More information about the Digitalmars-d
mailing list