Thread overview | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 29, 2004 Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Well, now we have opPos() to complement opNeg(). We can overload the unary plus operator. The obvious implementation is: # T opPos() { return this; } (or .dup, or similar). But such trivial functions do seem a little pointless. What /else/ could we do with opPlus()? This is a non-serious thread, so humorous replies are welcome! Jill |
July 29, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | Arcane Jill wrote:
> Well, now we have opPos() to complement opNeg(). We can overload the unary
> plus operator.
>
> The obvious implementation is:
>
> # T opPos() { return this; }
>
> (or .dup, or similar). But such trivial functions do seem a little
> pointless. What /else/ could we do with opPlus()?
>
> This is a non-serious thread, so humorous replies are welcome! Jill
You can use it to put some random obfuscated and lengthy code to impress your boss and made him think you work a lot instead of reading the D newsgroups.
|
July 29, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | In article <ceamke$1bfo$1@digitaldaemon.com>, Arcane Jill says... >(or .dup, or similar). But such trivial functions do seem a little pointless. >What /else/ could we do with opPlus()? > >This is a non-serious thread, so humorous replies are welcome! Jill Actually, this might be the answer Matthew was looking for since we cannot overload the pointer-to operator (*). > class Boxed(T){ > T value; > this(T value){ this.value = value; } > T opPos(){ return(this.value); } > } In this way, opPos now returns the underlying value, which could be quite a boon for template programming as this helps objects look more like primitives. > Boxed!(int) intBox = new Boxed!(int)(42); > int fortyTwo = 42; > assert(+intBox == +fortyTwo); - Pragma |
July 29, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | In article <ceavql$1ej2$1@digitaldaemon.com>, pragma <EricAnderton at yahoo dot com> says... > >Actually, this might be the answer Matthew was looking for since we cannot overload the pointer-to operator (*). > >> class Boxed(T){ >> T value; >> this(T value){ this.value = value; } >> T opPos(){ return(this.value); } >> } > >In this way, opPos now returns the underlying value, which could be quite a boon for template programming as this helps objects look more like primitives. > >> Boxed!(int) intBox = new Boxed!(int)(42); >> int fortyTwo = 42; >> assert(+intBox == +fortyTwo); Let's think: 1. it's barely readable 2. hmmm, ++ for double dereference ? by the way, why can't we overload the * unary operator ? |
July 29, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | Arcane Jill wrote:
> Well, now we have opPos() to complement opNeg(). We can overload the unary plus
> operator.
>
> The obvious implementation is:
>
> # T opPos() { return this; }
>
> (or .dup, or similar). But such trivial functions do seem a little pointless.
> What /else/ could we do with opPlus()?
>
> This is a non-serious thread, so humorous replies are welcome!
T opPos() { return opNeg(); }
It also might be useful in situations where one wants to use overloaded operators to create syntax that resembles another language. (like how Boost.Spirit apes BNF to create parsers)
-- andy
|
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | Sorry for being serious'n'all but aren't opPos and opNeg perfectly useful if you're implementing something that can be positive or negative? Like your Int class Jill? eg. class SimpleInt { int value; SimpleInt opPos() { if (value < 0) value = -value; return this; } SimpleInt opNeg() { if (value > 0) value = -value; return this; } } or am I missing something? Regan On Thu, 29 Jul 2004 11:21:50 +0000 (UTC), Arcane Jill <Arcane_member@pathlink.com> wrote: > Well, now we have opPos() to complement opNeg(). We can overload the unary plus > operator. > > The obvious implementation is: > > # T opPos() { return this; } > > (or .dup, or similar). But such trivial functions do seem a little pointless. > What /else/ could we do with opPlus()? > > This is a non-serious thread, so humorous replies are welcome! > Jill > > -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | *techincally* opPos returns the number. it's like multiplying by 1. it does nothing. but what you've just done is made it operate like abs(n) ;) also opNeg is like multiplying by -1, what you've done is like 0-abs(n). or we could overload opPos to do something horrible, like cause an access violation. or play some country music :o |
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | Arcane Jill wrote:
> Well, now we have opPos() to complement opNeg(). We can overload the unary plus
> operator.
>
> The obvious implementation is:
>
> # T opPos() { return this; }
>
> (or .dup, or similar). But such trivial functions do seem a little pointless.
> What /else/ could we do with opPlus()?
>
> This is a non-serious thread, so humorous replies are welcome!
The most obvious thing in the world:
void opPos() { this += 0.5; }
-- andy
|
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | Andy Friesen wrote:
> Arcane Jill wrote:
>> Well, now we have opPos() to complement opNeg(). We can overload the
>> unary plus operator.
>>
>> The obvious implementation is:
>>
>> # T opPos() { return this; }
>>
>> (or .dup, or similar). But such trivial functions do seem a little
>> pointless. What /else/ could we do with opPlus()?
>>
>> This is a non-serious thread, so humorous replies are welcome!
>
> T opPos() { return opNeg(); }
>
> It also might be useful in situations where one wants to use overloaded operators to create syntax that resembles another language. (like how Boost.Spirit apes BNF to create parsers)
Walter makes it very clear in the specs that he is against that practice. So be prepared for some major obstacles trying to follow that road...
|
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | On Fri, 30 Jul 2004 00:59:06 -0400, Jarrett Billingsley <kb3ctd2@yahoo.com> wrote: > *techincally* opPos returns the number. it's like multiplying by 1. it > does nothing. but what you've just done is made it operate like abs(n) ;) Thanks, I have never used opPos (operator+) nor have I used it on an int or anything like that so I had no idea what it did. I assumed it did something useful. It appears to be useless? > also opNeg is like multiplying by -1, what you've done is like 0-abs(n). Oops so I have, I should have realised that. > or we could overload opPos to do something horrible, like cause an access > violation. or play some country music :o If it's as useless as it appears to me.. the only thing stopping you is the sheer lunacy of it. Regan. -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ |
Copyright © 1999-2021 by the D Language Foundation