+1  A: 

I think the advice is not meant for scala especially, but for OO in general (so far I know scala is supposed to be a best-of-breed between OO and functional).

Overriding is fine, it's the heart of polymorphism and is central to OO design.

Overloading on the other hand is more problematic. With method overloading it's hard to discern which method will be really invoked and it's indeed a frequently a source of confusion. There is also rarely a justification why overloading is really necessary. The problem can most of the time be solved another way and I agree that overloading is a smell.

Here is an article that explain nicely what I mean with "overloading is a source of confusion", which I think is the prime reason why it's discouraged. It's for java but I think it applies to scala as well.

ewernli
-1, Firstly... WTF? What part of my post gives you impression that I confused between overriding and overloading? Did you even bother to check the link that I have posted? Secondly, this is a very unhelpful answer. I don't see you have made any point here.
missingfaktor
And Scala isn't primarily an OO language anyway.
Daniel Earwicker
@Rahul @Daniel Yes, I checked the post which just states "Avoid method overloading". Scala is supposed to be a best-of-breed between OO and functional, so far I know. Given the link, my guess is that the author of this advice advocates for not using overloading, just like it's discouraged in pretty much all OO languages. I didn't meant to be offensive, just trying to help. Still, it's your right to not like my answer.
ewernli
Well if you wouldn't have treated me like a novice who doesn't understand the difference between overriding and overloading then perhaps I wouldn't have downvoted you.
missingfaktor
@Rahul I'm sorry for that, that was not my intention. It's always hard to know the background people have in SO, even with the reputation. If you know the difference and you think there is another explanation, just don't take my answer into consideration.
ewernli
ewernli
@ewernli: That's a useful link. Edit your answer to include this link and I'll upvote you.
missingfaktor
@ewenli - Jorge is well known in the scala community and the link that Rahul provided was one of Jorge's scala tips, yet your answer has nothing to offer on why overloading is bad *specifically for scala*, which was clearly the intent of the question. Also, I have no idea why you decided that the question was confused in any way - you should just remove this from your answer as it's totally unjustified. -1
oxbow_lakes
@oxbow_lakes I already apologized for the tone of the answer that was not the best appropriate. I usually don't edit the answer when there are comments, because then the discussion makes no sense. Still, I've followed your advice in this case and I've rephrased it to express my original intent which is that I don't think the advice apply to scala especially, but is a common rule of thumb in any OO language. You may disagree and think there is a specific reason in the context of scala, but my answer should be ok now anyway.
ewernli
@Rahul I've added the link as well. Hope the answer is appropriate now. Lesson learned.
ewernli
@ewernli: Upvoted. Be careful next time.
missingfaktor
@Daniel Scala _is_ primarily an OO language. Any reason why you would not think so?
Daniel
@Daniel - because (like C++ and C#) it's a *multi-paradigm* language. Its authors describe it as such, as does its wikipedia page. Its unique selling point on its main target platform, the JVM, is that it provides functional programming facilities integrated with the OO features needed to interoperate with other JVM languages. The primary OO language on the JVM is Java (this hardly needs stating) and Scala is of interest primarily for enabling functional programming on the JVM.
Daniel Earwicker
@Daniel That's your point of view, and I dare to disagree. If you only want functional programming on the JMV, go for Clojure. Scala is really an attempt to marry OO and functional, for instance with case class to do something similar to pattern-matching.
ewernli
@ewernli - No, you agree with me! I said it's a multi-paradigm language, as do the *authors* of the language, and you say the same thing. You and I are *disagreement* with people calling it primarily an OO language.
Daniel Earwicker
@Daniel Seems like I really have communication issue on this whole question :) If when you write "Scala is of interest primarily for enabling functional programming" you mean "Scala is of interest for enabling functional programming *and* OO", yes I do agree :)
ewernli
Scala doesn't enable OO, because it aims to be a first class language on the JVM, and so it is *required* to be OO - it could not be anything but, and the JVM already has a massively well known default language - Java - that provides OO. So to say that Scala enables OO, as if it were impossible without Scala, would be very strange. What a language "enables" on its target platform is that which the platform itself does not already provide. In the case of Scala, that is functional programming. This does not mean that it is not an OO language. It means it is a multi-paradigm language.
Daniel Earwicker
@Daniel Well, Clojure is definitively not OO despite the fact that it runs on the JVM; a languge can have a semantics different than the JVM itself. But I still see what you mean and agree. Last remark you write "You and I are *disagreement* with people calling it primarily an OO language." but your 2nd comment was "And Scala isn't primarily an OO language anyway." Huh?
ewernli
In fact, I think Scala is even much more OO than Java. Think about it, everything is object in Scala (no primitive type, even first-class function is object, awesome!). There is no static member, just singleton object (even in Ruby, which everyone says it's a pure OO langauge, also have class method/variable too). There is even no operator, just method calls. How can you say that Scala is not primarily an OO language? ;)
Brian Hsu
@Daniel Earwicker: There are even people who refuse to call Scala a functional language. For instance, see http://enfranchisedmind.com/blog/posts/scala-not-functional/, http://enfranchisedmind.com/blog/posts/post-functional-scala/ and http://www.codecommit.com/blog/scala/is-scala-not-functional-enough ;-)
missingfaktor
Oh, the creator of a *competing* functional language doesn't think Scala is functional enough! Big shock! :)
Daniel Earwicker
@ewernlin - What's giving you difficulty with those two statements? Both are ways of saying that if someone characterises Scala mostly (or even solely) in terms of OO, they are missing the reason why anyone pays attention to it. It lets you adopt functional techniques without abandoning your favourite OO ecosystem, whereas a pure functional language wouldn't mesh so well with everything that has already been built in Java. That's Scala's raison d'être in a nutshell.
Daniel Earwicker
@Brian - all that is true, but it just means Scala is a better OO language than Java. I could well believe that just happened by accident. :)
Daniel Earwicker
@Daniel Ok. I understand now what you mean by *primarily*. You use it in contrast to the fact that it's an *auxiliary* characteristics of Scala (for me "isn't primarily" means "is not at all", but that's maybe because I'm not native English speaker). Thanks for the clarification.
ewernli
@Daniel Scala might be multi-paradigm, but it is still _primarily_ an Object Oriented language. The functional characteristics of Scala are implemented _as_ object oriented features. Even the most functional Scala program will be composed solely of objects and its respectives classes and traits. Now, Martin Odersky may say whatever he wants about his language (it's _his_ language, after all), but, in a strictly technical evaluation, Scala is primarily object oriented, where by "primarily" I mean that everything else is built upon this characteristic.
Daniel
I think what Daniel really want to express is that "Scala is not just an OO language, but also a functional one". Is that right? I'm not a native English speaker, but when I first saw the word "primarily", I think exactly what Daniel points out. Every functional feature in Scala is built upon OO, so in fact OO is the base of Scala, any other paradigm scala support is built unpon it. And just like ewernli, I also think "isn't primarily" means "is not at all" when I first saw this comment. Maybe because I'm not native English speaker just like him.
Brian Hsu
+23  A: 

Overloading makes it a little harder to lift a method to a function:

object A {
   def foo(a: Int) = 0
   def foo(b: Boolean) = 0
   def foo(a: Int, b: Int) = 0

   val function = foo _ // fails, must use = foo(_, _) or (a: Int) => foo(a)
}

You cannot selectively import one of a set of overloaded methods.

There is a greater chance that ambiguity will arise when trying to apply implicit views to adapt the arguments to the parameter types:

scala> implicit def S2B(s: String) = !s.isEmpty                             
S2B: (s: String)Boolean

scala> implicit def S2I(s: String) = s.length                               
S2I: (s: String)Int

scala> object test { def foo(a: Int) = 0; def foo(b: Boolean) = 1; foo("") }
<console>:15: error: ambiguous reference to overloaded definition,
both method foo in object test of type (b: Boolean)Int
and  method foo in object test of type (a: Int)Int
match argument types (java.lang.String)
       object test { def foo(a: Int) = 0; def foo(b: Boolean) = 1; foo("") }

It can quietly render default parameters unusable:

object test { 
    def foo(a: Int) = 0; 
    def foo(a: Int, b: Int = 0) = 1 
}

Individually, these reasons don't compel you to completely shun overloading. I feel like I'm missing some bigger problems.

UPDATE

The evidence is stacking up.

UPDATE 2

  • You can't (currently) use overloaded methods in package objects.
  • Applicability errors are harder to diagnose for callers of your API.
retronym
+8  A: 

Gilad Bracha had a nice post explaining this a while back:

http://gbracha.blogspot.com/2009/09/systemic-overload.html

Mushtaq Ahmed
+1, very convincing post.
missingfaktor
I'm not convinced by it at all. Barry Kelly (on the comments to that post) has it right. To clarify: if you need to define an operation for a fixed set of unrelated types (i.e. related only by inheriting `Object`), how is it advantageous to give all variants of that operation different names?! This is *the* major use case for overloading. And if they are final/sealed classes then there are no ambiguities. So it's a classic example of someone finding gotchas in 5% of situations and so declaring the language feature to be dangerous in the other 95% also (where in fact it adds value).
Daniel Earwicker
+3  A: 

The reasons that Gilad and Jason (retronym) give are all very good reasons to avoid overloading if possible. Gilad's reasons focus on why overloading is problematic in general, whereas Jason's reasons focus on why it's problematic in the context of other Scala features.

To Jason's list, I would add that overloading interacts poorly with type inference. Consider:

val x = ...
foo(x)

A change in the inferred type of x could alter which foo method gets called. The value of x need not change, just the inferred type of x, which could happen for all sorts of reasons.

For all of the reasons given (and a few more I'm sure I'm forgetting), I think method overloading should be used as sparingly as possible.

Jorge Ortiz