Is it possible to "overload" based on visibility?

Steven Schveighoffer schveiguy at gmail.com
Fri Sep 25 13:15:27 UTC 2020


On 9/25/20 3:43 AM, 60rntogo wrote:
> On Wednesday, 23 September 2020 at 19:27:13 UTC, Steven Schveighoffer 
> wrote:
>> This is a bug in the language.
> 
> Is this a known bug? If not, it should be reported.

I don't know, you can search for and report it here: 
https://issues.dlang.org

> 
> I came up with an answer to my original question that sort of works:
> 
> ---
> module foo;
> 
> struct Foo
> {
>    private int x;
> }
> 
> int x(Foo f)
> {
>    return f.x;
> }
> ---
> 
> The downside is that if I don't want to import all of foo at once, then 
> I have to import both Foo and x, but then I can read x from outside the 
> module and modify it form inside as I wanted. Are there any drawbacks of 
> this approach that I'm not seeing?

Wow, this is actually quite clever! I think it's a very valid solution. 
The only thing I would caution is that it takes Foo by value, which 
means it's going to make a copy of everything. Your toy example, that's 
OK, but if Foo is complex or has a significant copy constructor, it 
might be slow.

You can use auto ref to alleviate that:

int x()(auto ref Foo f) // needs to be a template for auto ref to work

-Steve


More information about the Digitalmars-d-learn mailing list