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()?
char[] opPos() { return "The glass is half full" };
|
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opsbxhk9m95a2sq9@digitalmars.com>, Regan Heath says... > >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? Clearly. (Sorry). I'd want x = -y; to assign x with minus y, not with -abs(y). If y is -3 then the statement x = -y; should (and will) result in (x == 3). And there is no way I'm going to change that. Int does provide the function abs(). In math, the main use for unary plus is to turn a double-valued result into a single-valued result. For instance sqrt(2) is double-valued, but +sqrt(2) is single-valued. It's hard to see how that could be useful in D. Jill |
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | In article <cecmgc$26dk$1@digitaldaemon.com>, Andy Friesen says... > >The most obvious thing in the world: > > void opPos() { this += 0.5; } > LOL. Excellent! Jill |
July 30, 2004 Re: Fun with opPos() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | "Arcane Jill" <Arcane_member@pathlink.com> skrev i en meddelelse news:ceamke$1bfo$1@digitaldaemon.com... > Well, now we have opPos() to complement opNeg(). We can overload the unary plus > operator. > (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! It is an obvious candidate for hiding easter eggs in libraries. Regards, Martin M. Pedersen |
July 31, 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 > By "fun" I guess you mean something evil like: struct Test { Test opPos() { printf("+\n"); return *this; } Test opNeg() { printf("-\n"); return *this; } } void main() { Test t; + - + + +t; } I guess it could have all kinds of evil-non-mathematical uses. -- -Anderson: http://badmama.com.au/~anderson/ |
July 31, 2004 opPos? (was Re: Fun with opPos()) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arcane Jill | "Arcane Jill" <Arcane_member@pathlink.com> escribió en el mensaje news:ceamke$1bfo$1@digitaldaemon.com | 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 Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus? ----------------------- Carlos Santander Bernal |
July 31, 2004 Re: opPos? (was Re: Fun with opPos()) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote: >"Arcane Jill" <Arcane_member@pathlink.com> escribió en el mensaje >news:ceamke$1bfo$1@digitaldaemon.com >| 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 > >Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus? > >----------------------- >Carlos Santander Bernal > > > opPlus implies that we are adding something. opPos means positive like opNeg means negative. -- -Anderson: http://badmama.com.au/~anderson/ |
July 31, 2004 Re: opPos? (was Re: Fun with opPos()) | ||||
---|---|---|---|---|
| ||||
Posted in reply to J Anderson | "J Anderson" <REMOVEanderson@badmama.com.au> escribió en el mensaje news:cegcuo$13du$1@digitaldaemon.com | Carlos Santander B. wrote: || Why is it opPos? Shouldn't it be opUnaryPlus, or even opPlus? || || ----------------------- || Carlos Santander Bernal || || || | opPlus implies that we are adding something. opPos means positive like | opNeg means negative. | | -- | -Anderson: http://badmama.com.au/~anderson/ But as Jill already pointed: +x = x, -x = -abs(x). (mathematically speaking), so it's not positive: it's the same ----------------------- Carlos Santander Bernal |
August 01, 2004 Re: opPos? (was Re: Fun with opPos()) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carlos Santander B. | Carlos Santander B. wrote:
> But as Jill already pointed: +x = x, -x = -abs(x). (mathematically speaking), so
> it's not positive: it's the same
It's not true that -x = -abs(x).
If x = -5, then -x = 5, but -abs(x) = -5.
WHAT AN IMPORTANT RESULT! ;)
James McComb
|
August 01, 2004 Re: opPos? (was Re: Fun with opPos()) | ||||
---|---|---|---|---|
| ||||
Posted in reply to James McComb | "James McComb" <alan@jamesmccomb.id.au> escribió en el mensaje news:cehlgr$1ju0$1@digitaldaemon.com | It's not true that -x = -abs(x). | | If x = -5, then -x = 5, but -abs(x) = -5. | | WHAT AN IMPORTANT RESULT! ;) | | James McComb (where do I hide, where do I hide????) Yes, sorry, my mistake. But I still think opPos is not the right name, for the same reason that +x != abs(x). But I won't argue for that anymore. (and to think that until a couple of years ago I was a very good mathemathician...) ----------------------- Carlos Santander Bernal |
Copyright © 1999-2021 by the D Language Foundation