Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
July 02, 2014 overloading InExpression | ||||
---|---|---|---|---|
| ||||
Is this possible? The documentation for std.container lists "in" as an operator in the container API but only associative arrays actually seem to support it. |
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | struct S { int opIn_r(int key) { return key*2; } } void main() { assert((42 in S.init) == 84); } |
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote:
> struct S
> {
> int opIn_r(int key)
> {
> return key*2;
> }
> }
>
> void main()
> {
> assert((42 in S.init) == 84);
> }
Thanks! I wonder, why the _r and lack of documentation?
|
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On 07/02/2014 07:35 AM, Vlad Levenfeld wrote: > On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote: >> struct S >> { >> int opIn_r(int key) >> { >> return key*2; >> } >> } >> >> void main() >> { >> assert((42 in S.init) == 84); >> } > > Thanks! I wonder, why the _r I think it is the old syntax, meaning "this is the overload for when an S object is on the right-hand side". > and lack of documentation? It appears in the binary operator table: http://dlang.org/operatoroverloading.html#Binary Also see the section 'Inclusion query by opBinaryRight!"in"' here: http://ddili.org/ders/d.en/operator_overloading.html Ali |
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Wednesday, 2 July 2014 at 14:35:55 UTC, Vlad Levenfeld wrote: > On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote: >> struct S >> { >> int opIn_r(int key) >> { >> return key*2; >> } >> } >> >> void main() >> { >> assert((42 in S.init) == 84); >> } > > Thanks! I wonder, why the _r and lack of documentation? Maybe something from old days? But in current <a href="http://dlang.org/operatoroverloading.html#Binary" target="_blank">doc</a> there is a opBinary: struct S { int opBinaryRight(string op : "in")(int key) { return key*2; } } void main() { assert((42 in S.init) == 84); } |
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kozzi11 | On Wednesday, 2 July 2014 at 15:36:23 UTC, Kozzi11 wrote:
>> Thanks! I wonder, why the _r and lack of documentation?
>
> Maybe something from old days? But in current <a
> href="http://dlang.org/operatoroverloading.html#Binary"
> target="_blank">doc</a> there is a opBinary:
Yep, I think it is D1 legacy approach. opBinary should be more appropriate.
|
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | Dicebot:
> Yep, I think it is D1 legacy approach. opBinary should be more appropriate.
I hope the usage of the old operator overloading functions will generate deprecation messages soon.
Bye,
bearophile
|
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Wed, Jul 02, 2014 at 02:35:54PM +0000, Vlad Levenfeld via Digitalmars-d-learn wrote: > On Wednesday, 2 July 2014 at 14:14:57 UTC, Dicebot wrote: > >struct S > >{ > > int opIn_r(int key) > > { > > return key*2; > > } > >} > > > >void main() > >{ > > assert((42 in S.init) == 84); > >} > > Thanks! I wonder, why the _r and lack of documentation? Don't use opIn_r, that's a relic from D1. Instead, use this: struct S { int opBinaryRight(string op)(int key) if (op == "in") { return key*2; } } T -- I think Debian's doing something wrong, `apt-get install pesticide', doesn't seem to remove the bugs on my system! -- Mike Dresser |
July 02, 2014 Re: overloading InExpression | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | Ah yes I never noticed that "in" was in the binary op table. In my defense, searching for "in" usually yields too many results to be useful. Thanks to everyone for your help! |
Copyright © 1999-2021 by the D Language Foundation