Thread overview | |||||
---|---|---|---|---|---|
|
April 19, 2020 Wich: opIndex overloading by return type | ||||
---|---|---|---|---|
| ||||
Wouldn't it make a lot of sense to allow different opIndex implementations based on return type?
class myC {
myT1 opIndex(int x)
myT2 opIndex(int x)
}
Depending on the types involved myC[1] woudl return myT1 or myT2. Use-case: I have a geomentry object and in one case I get x0,y0,x1,y1 and in the other case x,y,w,h
IMO that would make a lot of sense.
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
|
April 19, 2020 Re: Wich: opIndex overloading by return type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | It is easy. You can make both types as children of common parent class myT, and when return myT. |
April 19, 2020 Re: Wich: opIndex overloading by return type | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | On 4/19/20 11:53 AM, Robert M. Münch wrote:
> Wouldn't it make a lot of sense to allow different opIndex implementations based on return type?
>
> class myC {
> myT1 opIndex(int x)
> myT2 opIndex(int x)
> }
>
> Depending on the types involved myC[1] woudl return myT1 or myT2. Use-case: I have a geomentry object and in one case I get x0,y0,x1,y1 and in the other case x,y,w,h
>
> IMO that would make a lot of sense.
>
D doesn't do overloading based on return type.
You can do some things like return an item that alias this'd to one of the two, and then use an accessor for the other.
Or you could provide indexers that operate with those types. e.g:
myC.asT1[1] => T1
myC.asT2[1] => T2
-Steve
|
Copyright © 1999-2021 by the D Language Foundation