
- 90,905
- 62
- 285
- 365
-
Could it have anything to do with the fact that Scala inherits Java's type erasure? – Justin Niessner Mar 24 '10 at 17:55
-
1@Justin: What does type erasure have got to do with this? – missingfaktor Mar 24 '10 at 17:58
-
6Why don't you ask Jorge Ortiz why he advises against method overloading? – John Mar 24 '10 at 18:23
-
Not sure if it's applicable since I don't know Jorge's original intent, but: http://michid.wordpress.com/2008/02/08/implicit-double-dispatch-revisited/ – Justin Niessner Mar 24 '10 at 19:36
-
@Rahul G - The article has pros and cons, actually. As was mentioned, it is hard to know why someone has an opinion. – James Black Mar 25 '10 at 02:25
-
13*bah...* http://bit.ly/aduyIn :'( – missingfaktor Mar 26 '10 at 02:21
-
The link to Jorge Ortiz aritcle seems to be broken. Is it still available somewhere? – Roland Feb 09 '17 at 13:43
-
@Roland, I think it's gone. – missingfaktor Feb 09 '17 at 15:03
-
Who is Jorge Ortiz? – Per Lundberg Sep 15 '20 at 17:45
-
@Per The linked post had enough context once but I don't take the responsibility of making sure links on the web, the ones I don't own, continue to work after 10 years. Sorry not sorry! – missingfaktor Sep 16 '20 at 18:09
-
@Per I'm sure if you really wanted to find out, you can, by googling "Jorge Ortiz Scala" etc. – missingfaktor Sep 16 '20 at 18:09
4 Answers
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.
- It complicates the spec
- It can render implicits unsuitable for use in view bounds.
- It limits you to introduce defaults for parameters on only one of the overloaded alternatives.
- Because the arguments will be typed without an expected type, you can't pass anonymous function literals like '_.foo' as arguments to overloaded methods.
UPDATE 2
- You can't (currently) use overloaded methods in package objects.
- Applicability errors are harder to diagnose for callers of your API.
UPDATE 3
- static overload resolution can rob an API of all type safety:
scala> object O { def apply[T](ts: T*) = (); def apply(f: (String => Int)) = () }
defined object O
scala> O((i: String) => f(i)) // oops, I meant to call the second overload but someone changed the return type of `f` when I wasn't looking...

- 9,904
- 3
- 37
- 61

- 54,768
- 12
- 155
- 168
-
2Currently, there is also a bug in scalac which is triggered by overloading in certain cases. https://issues.scala-lang.org/browse/SI-7596 . – cvogt Jun 21 '13 at 05:01
-
2First two issues don't affect every valid use of overloading. Filed [a bug report](https://issues.scala-lang.org/browse/SI-7856) for 3rd issue. The [restriction on defaults is by choice](http://stackoverflow.com/a/4652681/615784), and in theory could be fixed. Fault of the `_.foo` issue is Scala's limited type inference, not overloading. You answer the question, but some of the reasons are due to other weaknesses in Scala that could be improved. Overloading is more efficient than runtime downcasting a disjunction, or a Cartesian product of names is noisy and disconnects from a shared semantic. – Shelby Moore III Sep 18 '13 at 10:59
-
Type classes offer an alternative to overloading in some cases. The Magnet Pattern (http://spray.io/blog/2012-12-13-the-magnet-pattern/) pushes this idea in interesting ways. – retronym Sep 18 '13 at 16:12
-
But you are right, overloading in Scala by admission something of a second class features. It's needed for Java interop, expected by Java converts, but we can't make it work perfectly with every other feature of Scala, so in some cases we just levy restrictions. – retronym Sep 18 '13 at 16:13
-
3Typeclasses are currently [unable to be used generally due to lack of a first-class union type](https://issues.scala-lang.org/browse/SUGGEST-22?focusedCommentId=65935#comment-65935). Yeah I see that attitude in Scala's community, yet imagine instead `addIntToDouble`, `addDoubleToInt`, i.e. a Cartesian product of names instead of static typing for every common semantic. Replacing typing with naming seems to be regressive. [Java got more things correct](http://programmers.stackexchange.com/questions/176665/generics-and-type-erasure/212038#212038) than perhaps we recognize. – Shelby Moore III Sep 21 '13 at 03:36
-
2[I wrote](https://groups.google.com/d/msg/scala-debate/m6QuiSUP43A/Kpys2dVGSNcJ) in the discussion thread (for the bug report I mentioned in prior comment), "What is evil IMO is expecting overloading to be what it isn't, or diminishing the importance of having one name for a common semantic". – Shelby Moore III Sep 21 '13 at 03:40
-
2The answer and comments seem very naive to me and do not mention the single most important reason to use overloading: when a method must actually perform very different internal logic depending on the types passed to it. The answer of "use implicits" fails immediately, because there may not exist any possible conversion from one object to a different type that embodies the specialized logic. – ely Aug 16 '18 at 13:30
-
1Here's an example. Suppose you have a method `slowCalculation(v: Vector[Int])`. Now suppose you want to be able to pass `List[Vector[Int]]` to `slowCalculation` and have it perform the standard calculation sequentially for each vector in your list, then aggregate the result. A naive implicit idea would be to flatten the `List[Vector[Int]]` into a single huge vector then allow the original function to operate on that, but there can be memory issues, complexity issues, etc. Now imagine it is `List[MyType]`, where conversion between `MyType` and `Vector` is hugely costly... – ely Aug 16 '18 at 13:33
-
1Sometimes you just want the caller to be able to pass whatever they have for the convenience and uniformity of the API (so writing these as wholly separate functions is super bad and kludgy), but you want to specialize the internal function logic to each type-specialized case, and crucially *not* try to do conversion behind the scenes. – ely Aug 16 '18 at 13:35
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.

- 4,722
- 1
- 21
- 22
-
2If you don't want that to happen, you declare the type for x. If you don't declare it, then you are saying you desire for it to change. The semantics of `foo` should be the same for every overload with the same number of parameters, else it was designed incorrectly. As for limiting the scope of bizarre cascade of inference changes, public methods should always declare their return types. I think this was one of the issues affecting Scala binary compatibility between versions. – Shelby Moore III Sep 18 '13 at 09:58
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.

- 38,045
- 5
- 92
- 123
-
2
-
@ewernli: That's a useful link. Edit your answer to include this link and I'll upvote you. – missingfaktor Mar 25 '10 at 02:38
-
4@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 Mar 25 '10 at 09:23
-
@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 Mar 25 '10 at 09:49
-
@Rahul I've added the link as well. Hope the answer is appropriate now. Lesson learned. – ewernli Mar 25 '10 at 09:53
-
7@Daniel Scala _is_ primarily an OO language. Any reason why you would not think so? – Daniel C. Sobral Mar 26 '10 at 00:59
-
@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 Mar 26 '10 at 08:09
-
1@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 Mar 26 '10 at 08:24
-
@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 Mar 26 '10 at 08:28
-
@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 Mar 26 '10 at 08:37
-
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 Mar 26 '10 at 09:03
-
1@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 Mar 26 '10 at 09:14
-
1In 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 Mar 26 '10 at 09:14
-
@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 Mar 26 '10 at 09:23
-
3Oh, the creator of a *competing* functional language doesn't think Scala is functional enough! Big shock! :) – Daniel Earwicker Mar 26 '10 at 09:52
-
1@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 Mar 26 '10 at 09:57
-
@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 Mar 26 '10 at 09:58
-
@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 Mar 26 '10 at 10:12
-
3@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 C. Sobral Mar 26 '10 at 22:50
-
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 Mar 27 '10 at 00:07
-
Most of what Daniel (Earwicker) really wants to express is wrong. Scala *is* primarily an OO language -- every value is an object, the majority of features are OO, the library is heavily OO. And the ability to adopt functional techniques on the JVM is not Scala's raison d'être -- Clojure does that, and Scala is also targeted to .NET where F# among other languages live. If Scala were what Daniel says it is, it would look very different and be marketed very differently it would have had a different designer -- Martin Odersky is far from a pure FP guy. – Jim Balter Jul 27 '11 at 08:56
-
1The linked article is nonsense! He iterates a collection of type `Collection`, then he wonders why the static overload that inputs a `Collection` is called. Perhaps he should learn to code, and understand that `classify` either needs to downcast at runtime or use an overrideable method as he showed for `name`. He knows this obviously and thus he knows he is writing nonsense. – Shelby Moore III Sep 18 '13 at 10:11
-
I think it's probably most fair to say that Scala is an object-functional language, at least in spirit. It's based around many FP techniques, but using modern OOP heavily in its typing system and as a tool for organization. Underlying its design are many techniques pioneered in the functional world, like defining the semantics of the language using calculi. Javascript and Ruby, by comparison, seem to have been influenced by FP, but designed much moreso like typical imperative languages. – acjay Jun 09 '14 at 05:15
I recently wrote a blog about this very problem: The Hidden Dangers of Method Overloading. I believe that method overloading may negatively impact code quality because maintaining two or more implementations under the same name gives rise to concealed semantics, which inevitably result in misunderstandings and functional defects.

- 102,010
- 123
- 446
- 597