UCFS does not work for nested functions?

Steven Schveighoffer schveiguy at yahoo.com
Mon Jun 18 19:26:47 UTC 2018


On 6/18/18 2:58 PM, aliak wrote:
> On Monday, 18 June 2018 at 17:58:11 UTC, Steven Schveighoffer wrote:
>> What then can happen is that your local calls can get hijacked from 
>> outside the module, if someone happens to define something later that 
>> you happened to import. D tries to avoid such possibilities.
>>
>> There's not much precedent for local symbols being overridden by 
>> module-level symbols.
> 
> I thought that happens already with non-nested functions:
> 
> module a;
> 
> struct A {
>    void f(); // assume it's added later
> }
> 
> module b;
> import a;
> 
> void f(A) { }
> void g() {
>    auto x = A();
>    a.f(); // this would be calling local f until someone added A.f
> }
> 
> Or I misunderstood what you said?

It's the same in the fact that your call is silently switched to a 
different call. However, in the current syntax, an external entity 
CANNOT override a local function. When you call the nested function, 
it's the nested function, no matter what else occurs outside (even in 
the local module). There is no precedent for local functions to be 
overridden by module-level functions.

So if we allow this, we break a guarantee of which function is called, 
albeit via a different syntax. Generally, the local function takes 
precedence, then the member functions, then module-level functions. 
Making the module level functions override the local functions is not 
normal or expected. Generally you are defining locals to override what 
you see outside the function.

But with UFCS, it's treated as part of the API of the type. The type 
defines the API first, and then you can add to it, you can't override it.

Part of this is historical in nature -- UFCS came after member 
functions, and so they had to be lower priority.

> PS: This is something I've worried about before actually [1] when I was 
> more of a noob than now, but I've come to accept I guess :) .... though 
> I could still be misunderstanding things of course :/
> 
> https://forum.dlang.org/post/crcbaautgmrglhzvxmvk@forum.dlang.org

I think the current state of affairs still leaves some hijacking doors 
open, depending on your point of view. It's certainly not perfect. The 
only way to be sure you are doing things correctly is to use member 
syntax when you know it's a member, and function syntax otherwise.

IMO, UFCS for locals isn't going to change, but I could also be wrong. 
It's not really up to me what goes into the language, I just am trying 
to help explain the rationale behind the current rules.

-Steve


More information about the Digitalmars-d-learn mailing list