Is it possible to "overload" based on visibility?

Steven Schveighoffer schveiguy at gmail.com
Fri Sep 25 15:21:22 UTC 2020


On 9/25/20 11:13 AM, 60rntogo wrote:
> On Friday, 25 September 2020 at 14:21:59 UTC, Steven Schveighoffer wrote:
>> in does not mean "take by reference", it means "scope const"
> 
> I'm not sure that I really understand scope, but I read 
> https://dlang.org/spec/function.html#param-storage as saying "in means 
> take by value or reference depending on what is better optimized". Is 
> that not what we want here?

That's new, unreleased (available in 2.094.0), and requires the 
-preview=in switch. I wouldn't depend on that mechanism yes.

> Right, again I'm wondering if there is a way of saying "just figure out 
> if it's more optimal to return by value or const reference".

If the input is not ref, you should not return by ref, because then you 
would be returning a reference to local stack data that is about to be 
destroyed.

The only way to say "make this const ONLY if it's ref" is to overload 
the function with the proper attributes for the proper situations.

Otherwise, you can just return const auto ref.

-Steve


More information about the Digitalmars-d-learn mailing list