Jump to page: 1 24  
Page
Thread overview
Fun with opPos()
Jul 29, 2004
Arcane Jill
Jul 29, 2004
Juanjo Álvarez
Jul 29, 2004
pragma
Jul 29, 2004
h3r3tic
Jul 29, 2004
Andy Friesen
Jul 30, 2004
Norbert Nemec
Jul 30, 2004
Regan Heath
Jul 30, 2004
Regan Heath
Jul 30, 2004
Arcane Jill
Jul 30, 2004
Andy Friesen
Jul 30, 2004
Arcane Jill
Jul 30, 2004
Mike Parker
Jul 30, 2004
Martin M. Pedersen
Jul 31, 2004
J Anderson
opPos? (was Re: Fun with opPos())
Jul 31, 2004
J Anderson
Aug 01, 2004
James McComb
Aug 01, 2004
Arcane Jill
Aug 01, 2004
Andy Friesen
Aug 01, 2004
parabolis
Aug 01, 2004
Sean Kelly
Aug 01, 2004
parabolis
Aug 01, 2004
Arcane Jill
Aug 01, 2004
parabolis
Aug 02, 2004
h3r3tic
Aug 01, 2004
Nick
Aug 01, 2004
Sean Kelly
July 29, 2004
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
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
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
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
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
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
*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
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
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
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/
« First   ‹ Prev
1 2 3 4