Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Notorious b.s.d. posted:

no one is ever tempted to do the latter

operator overloading creates the temptation to do really stupid poo poo and there's always some idiot who gives in

in about a decade of using languages with operator overloading i don't think i've ever actually encountered a stupid operator overload that actively mislead me while reading code (at most, i've encountered stupid operator overloads that made it very clear that i didn't know what the code did without digging in further), but i've encountered a whole fuckload of normal functions with misleading names that did something completely unlike what the name suggests. sometimes the obvious non-operator name isn't wrong, but is ambigous. does list.append() mutate the list, or return a new list with the object added? depends on what language you're using, because i've encountered both. list + x obviously returns a new list, and list += x obviously mutates the list.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

fleshweasel posted:

another thing with operator overloading is: it's totally undiscoverable with IDE autocomplete, so people are probably just going to use the method version of it anyway.

i think this actually leads to a good rule of thumb for when to define operator overloads. if the overload actually makes sense, then you don't need discoverability since it's what the programmer will try first before looking for a method. if no normal user would just naturally start with writing a + b or a[b] and then get annoyed when that doesn't compile, then the operator is probably not a good name for that function.

FamDav
Mar 29, 2008
operator overloading is dumb because of precedence rules that constrain how you can create your dsl or whatever. haskell is the only one to get this right by letting you actually define arbitrary operators with your chosen fixity and binding rules. scala is actually the worst because it lets you define arbitrary operators but it picks precedence based on the first character.

FamDav
Mar 29, 2008

Barnyard Protein posted:

does someone have an article or rundown of the reasons operator overloading is bad? i have an intuitive feel of why its a bad thing, but i'd like to be well-versed in the reasons. a coworker was excitedly talking about operator overloading being a thing in java9

considering there is not a single jep related to operator overloading your coworker has no idea what he's talking about hth

fritz
Jul 26, 2003

fleshweasel posted:

another thing with operator overloading is: it's totally undiscoverable with IDE autocomplete, so people are probably just going to use the method version of it anyway.

i dont feel up to firing up an ide but wont it have full type info available, unless youre in something like python

brap
Aug 23, 2004

Grimey Drawer
it will type check your usage of the operator, but the typical way of finding functions that use a particular value is by hitting . and scrolling through the menu that pops up

jony neuemonic
Nov 13, 2009

FamDav posted:

scala is actually the worst because it lets you define arbitrary operators but it picks precedence based on the first character.

wait what?

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
Also typically method names will be easier to disambiguate when you're casually browsing some code, as opposed to the canonical Scalaz examples from a few years back, and some HTTP library name whose name I forgot, where you need to know the difference between operators =, ~=, ~, ~~, ~~# and whatever else.

FamDav
Mar 29, 2008

http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#infix-operations

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
operator overloading is fine and people complain about it too much. most of the complaints i see are either entirely hypothetical or people being scared of how a language they haven't used looks at first glance.

FamDav
Mar 29, 2008
the project that is going to keep scala relevant for a solid decade (apache spark) has a style guide that eschews most of the scala feature set. its the equivalent of the google c++ style guide.

they've also done a lot of work to refactor things to make even less use of features like implicits, and that kind of mentality is pushing into things like breeze because real people don't want to deal with that junk.

AWWNAW
Dec 30, 2008

f# set overloads + for union and - for difference which is way more readable especially in the case of difference when I'm always wondering which set is getting subtracted from

Vanadium
Jan 8, 2005

like ten years ago some dude i looked up to at the time showed me a C++ DSL he made with operator overloading that concisely constructed gtk windows with various widgets in them. It was pretty rad, dude's rad.

Vanadium
Jan 8, 2005

a.add(b) sending an email makes perfect sense b/c it sends b a notification that a added them to their contact list. duh.

Bloody
Mar 3, 2013

Barnyard Protein posted:

does someone have an article or rundown of the reasons operator overloading is bad? i have an intuitive feel of why its a bad thing, but i'd like to be well-versed in the reasons. a coworker was excitedly talking about operator overloading being a thing in java9


Shaggar posted:

operators have well understood meanings in some contexts (ex: adding ints, concatenating strings) but outside of that they could mean anything (ex: Person a + Person b =???)

Its much better to use descriptive method names.

there are some math related types that you might write where it might be reasonable to overload operators, but they're the exception.

qntm posted:

sometimes I overload subtraction to undo concatenation

"ABCDEF" - "DEF" returns "ABC"

CPColin posted:

Is that properly anticommutative? That is, does "DEF" - "ABCDEF" return "-ABC"?

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:


scala is amazing

Bloody
Mar 3, 2013

AWWNAW posted:

f# set overloads + for union and - for difference which is way more readable especially in the case of difference when I'm always wondering which set is getting subtracted from

a.Except(b) is a good way to spell it

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

lol, not only is precedence defined by the first character of the operator, but associativity is defined by the last character.

imagine if someone asked you to come up with an operator precedence/associativity system. would you have ever thought of this?

Bloody
Mar 3, 2013

its probably entirely coincidental behavior

Notorious b.s.d.
Jan 25, 2003

by Reene

Sagacity posted:

Also typically method names will be easier to disambiguate when you're casually browsing some code, as opposed to the canonical Scalaz examples from a few years back, and some HTTP library name whose name I forgot, where you need to know the difference between operators =, ~=, ~, ~~, ~~# and whatever else.

spray.

fuckin spray

also sbt

basically scala is an extended lesson in why operator overloading is bad

Notorious b.s.d.
Jan 25, 2003

by Reene

fart simpson posted:

imagine if someone asked you to come up with an operator precedence/associativity system. would you have ever thought of this?

i would bury this person in an unmarked grave and we would never discuss it again

it was a hard design decision but it had to be done

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
personally i'd be okay with operator overloading if it was only used to overload pure functions but then i'd have to trust other programmers to only overload pure functions and no one would ever use my operator overloads because they would be suspicious of them so it's not worth it

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
i am working on a query library and i have a thing off to the side that lets you write π, σ, etc for project, select and so on but i'm never going to include it

fritz
Jul 26, 2003

qntm posted:

sometimes I overload subtraction to undo concatenation

"ABCDEF" - "DEF" returns "ABC"
-"DEF" == inv(F)*inv(E)*inv(D) so it makes sense that

"ABCDEF" - "DEF" = "ABC", and so then "ABCDEF" - "E" = "ABCDEFinv(E)"

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

MALE SHOEGAZE posted:

personally i'd be okay with operator overloading if it was only used to overload pure functions but then i'd have to trust other programmers to only overload pure functions and no one would ever use my operator overloads because they would be suspicious of them so it's not worth it

looks like haskell wins again

VikingofRock
Aug 24, 2008




Just because some people misuse operator overloading doesn't mean that operator overloading itself is bad. People will misuse literally every feature you give them, and just because someone has used operator overloading on their email class doesn't mean that using operator overloading on your quaternion class is bad.

Personally I think Haskell does operator overloading right. For example, in order to overload the + operator, you need to define the ways in which your type is a numeric type. So you can easily overload the + operator for quaternions, but not for emails. (You can also define a whole new + operator, but then you have to specify which one you are using every time so no one does that.)

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

VikingofRock posted:

Just because some people misuse operator overloading doesn't mean that operator overloading itself is bad. People will misuse literally every feature you give them, and just because someone has used operator overloading on their email class doesn't mean that using operator overloading on your quaternion class is bad.

Personally I think Haskell does operator overloading right. For example, in order to overload the + operator, you need to define the ways in which your type is a numeric type. So you can easily overload the + operator for quaternions, but not for emails. (You can also define a whole new + operator, but then you have to specify which one you are using every time so no one does that.)

yeah, typeclasses handle this really well. if you want to overload + you have to overload *, abs, signum (sign), fromInteger, and negate. prevents the worst abuses of oo because overloading + can only sensibly be done on something resembling numbers.

gonadic io
Feb 16, 2011

>>=

HappyHippo posted:

yeah, typeclasses handle this really well. if you want to overload + you have to overload *, abs, signum (sign), fromInteger, and negate. prevents the worst abuses of oo because overloading + can only sensibly be done on something resembling numbers.

code:
instance Num b => Num (a -> b) where
      negate      = fmap negate
      (+)         = liftA2 (+)
      (*)         = liftA2 (*)
      fromInteger = pure . fromInteger
      abs         = fmap abs
      signum      = fmap signum
> (sin^2 + cos^2) 123.4
1.0

in fairness this is mostly a joke, i've never seen it actually used other than in irc's lambdabot

computer toucher
Jan 8, 2012

KARMA! posted:

php has this as well

code:

$foo = "butts";
$butts = "call me {$foo} man";

But of course php also has
code:

$foo = "butts";
$butts = sprintf("call me %s man", $foo);

or!

code:

$foo = "butts";
$butts = vprintf("call me %s man", [$foo]);

So you can choose! wow thanks php

or the prettiest option:

print "call me ".$foo." man";

Notorious b.s.d.
Jan 25, 2003

by Reene

VikingofRock posted:

Just because some people misuse operator overloading doesn't mean that operator overloading itself is bad. People will misuse literally every feature you give them, and just because someone has used operator overloading on their email class doesn't mean that using operator overloading on your quaternion class is bad.

yes, all syntactic features are a tradeoff between use and abuse. operator overloading offers minimal benefits, but creates a lot of temptation to abuse the language. it's just not worth it.

i would be perfectly happy to write "2.plus(2)" the rest of my days if it means i never have to look at a fuckup like "cout << dongs << butts" ever again

the talent deficit
Dec 20, 2003

self-deprecation is a very british trait, and problems can arise when the british attempt to do so with a foreign culture





Sagacity posted:

Also typically method names will be easier to disambiguate when you're casually browsing some code, as opposed to the canonical Scalaz examples from a few years back, and some HTTP library name whose name I forgot, where you need to know the difference between operators =, ~=, ~, ~~, ~~# and whatever else.

loving dispatch

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Notorious b.s.d. posted:

yes, all syntactic features are a tradeoff between use and abuse. operator overloading offers minimal benefits, but creates a lot of temptation to abuse the language. it's just not worth it.

i would be perfectly happy to write "2.plus(2)" the rest of my days if it means i never have to look at a fuckup like "cout << dongs << butts" ever again

1) oo offers great benefits when you actually need it because poo poo like adding vectors is downright painful when it isn't available
2) the temptation to abuse it is actually quite low - the syntax is often more complex and you often need to overload a bunch of operators. most people dont go through the trouble unless they really want it.
3) writing "2.plus(2)" is moronic. anyone who would want that just to prevent the c++ stream overloads (which aren't that bad) is insane. do you have ocd or something?

i seriously dont get why people get so drat upset over operator overloading of all things. its not that big a deal, even when it's misused.

Bloody
Mar 3, 2013

go look at a page of scala code

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Bloody posted:

go look at a page of scala code

ok so scala does it wrong (idk i dont use scala)? being poorly implemented in one language doesnt mean much

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
java hosed up generics doesn't mean generics are bad. in fact they're good

Valeyard
Mar 30, 2012


Grimey Drawer
Pythong

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

uncurable mlady posted:

r# taught me you can do string formatting like this -

var foo = "butts"
var bar = "call me {foo} man"

the downside of string interpolation is that it's harder to localize

you need to be able to have "call me {foo} man" (or equivalent) act as the key, and then have the replacement happen in the localized string, and have that be as transparent as possible to the developer

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

Notorious b.s.d. posted:

operator overloading creates the temptation to do really stupid poo poo and there's always some idiot who gives in

c4_View c4_View::operator,(const c4_Property &) const

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

FamDav posted:

operator overloading is dumb because of precedence rules that constrain how you can create your dsl or whatever. haskell is the only one to get this right by letting you actually define arbitrary operators with your chosen fixity and binding rules. scala is actually the worst because it lets you define arbitrary operators but it picks precedence based on the first character.

Lisp gets this even more right than Haskell by just always using prefix notation

(except in loving loop of course)

Adbot
ADBOT LOVES YOU

eschaton
Mar 7, 2007

Don't you just hate when you wind up in a store with people who are in a socioeconomic class that is pretty obviously about two levels lower than your own?

VikingofRock posted:

Just because some people misuse operator overloading doesn't mean that operator overloading itself is bad. People will misuse literally every feature you give them, and just because someone has used operator overloading on their email class doesn't mean that using operator overloading on your quaternion class is bad.

you will believe this right up until you actually have to use their email class in a project

  • Locked thread