May 11, 2012 Re: Should opIndex completely override alias this? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 05/11/12 19:47, Mehrdad wrote:
> On Friday, 11 May 2012 at 17:37:11 UTC, Artur Skawina wrote:
>> On 05/11/12 19:22, Mehrdad wrote:
>>> On Friday, 11 May 2012 at 17:19:28 UTC, kenji hara wrote:
>>>> This is expected behavior.
>>>>
>>>> 'alias this' works as proper super type.
>>>>
>>>> struct S { T[] data; alias data this; }
>>>>
>>>> In this code, S behaves as it is derived from T[] .
>>>>
>>>> Following to this view, the definition of opIndex in S overrides (and hides) T[]'s opIndex completely.
>>>>
>>>> Kenji Hara
>>>
>>> How do you overcome this, in the case where you don't know the data type of 'data' (so you don't know what the overloads might look like, maybe because it's a template)?
>>
>> Not quite sure what you mean, but
>>
>> auto opIndex(A...)(A a) { return data[a]; }
>>
> What if it's const/pure/@safe/whatever though? Or the parameters have some attributes?
Since it is a function template the attributes are inferred, in this case from data's opIndex, if any. The arguments are forwarded as-is, ie data's opIndex has to deal with them.
The return type could be 'auto ref' too, then returning by reference would also magically work, but this is not always desirable, as it could leak private references; making return-by-ref explicit is probably safer in cases like this one.
artur
| |||
May 11, 2012 Re: Should opIndex completely override alias this? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | On Friday, 11 May 2012 at 18:32:12 UTC, Artur Skawina wrote:
> Since it is a function template the attributes are inferred, in this case from data's opIndex, if any. The arguments are forwarded as-is, ie data's opIndex has to deal with them.
Among other things, 'scope' gets messed up.
| |||
May 11, 2012 Re: Should opIndex completely override alias this? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Mehrdad | On 05/11/12 20:40, Mehrdad wrote:
> On Friday, 11 May 2012 at 18:32:12 UTC, Artur Skawina wrote:
>> Since it is a function template the attributes are inferred, in this case from data's opIndex, if any. The arguments are forwarded as-is, ie data's opIndex has to deal with them.
>
>
> Among other things, 'scope' gets messed up.
How? By making the forwarder not callable w/ a scoped argument, when the target function would be? It isn't a problem for operator overloading, as the signature(s) are known. But in general, scope-inference would be needed. Which has similar problems as 'uniq', for non-trivial function bodies.
Yes, things aren't perfect; eg 'this' attributes/classes like shared or const also need explicit handling (recent frontend changes apparently improved the situation a bit).
artur
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply