14

I have the following:

def calc(dir: File): Option[Group] = {
 calcModel(dir) match {
  case Some(model: Model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)
  case None =>               None
 }
}

def calcUI(model: Model, availableWidth: Double, availableHeight: Double, maxLevel: Int): Option[Group] = {
}

def calcUI(model: Model, posX: Double, posY: Double, availableWidth: Double, availableHeight: Double, horizontally: Boolean, startLevel: Int, maxLevel: Int): Option[Group] = {
}

protected def calcUI(node: Node, posX: Double, posY: Double, availableWidth: Double, availableHeight: Double, horizontally: Boolean, level: Int, maxLevel: Int): Group = {
}

def calcModel(dir: File): Option[Model] = {
}

(Remark: Model does NOT derive from Node)

and get the following compiler error message:

Error:(88, 27) overloaded method value calcUI with alternatives:
  (node: org.rob.spaceview.modell.Node,posX: Double,posY: Double,availableWidth: Double,availableHeight: Double,horizontally: Boolean,level: Int,maxLevel: Int)javafx.scene.Group <and>
  (model: org.rob.spaceview.modell.Model,posX: Double,posY: Double,availableWidth: Double,availableHeight: Double,horizontally: Boolean,startLevel: Int,maxLevel: Int)Option[javafx.scene.Group] <and>
  (model: org.rob.spaceview.modell.Model,availableWidth: Double,availableHeight: Double,maxLevel: Int)Option[javafx.scene.Group]
 cannot be applied to (org.rob.spaceview.modell.Model, Double, Double, Int, Int)
      case Some(model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)
                      ^

I do not get it. All calcUI functions are different by parameters.

I know this error and most of the time I get it and fix it. But here, no clue.

Hopefully somebody enlights me. Thanks.

Lomig Mégard
  • 1,828
  • 14
  • 18
Rob
  • 191
  • 1
  • 1
  • 7
  • I don't get it, either. Which method do you expect to select? You use five args. You know overloads are evil, right? At least, unless you know what you're doing? – som-snytt Mar 14 '14 at 08:41
  • 3
    Overloads are evil? I would say overloading is an extremely useful and important aspect of Java/Scala. – Keppil Mar 14 '14 at 08:43
  • See the answer and my comment below. Shame on me. – Rob Mar 14 '14 at 08:56
  • @Keppil E.g. http://stackoverflow.com/q/2510108/1296806 or google it. Also, I can't reply if you don't use @. Even if it is a necessary evil (debatable), still evil. – som-snytt Mar 14 '14 at 08:56
  • @som: Interesting link, thank you. – Keppil Mar 14 '14 at 09:40

1 Answers1

13

Actually, the given parameters in the call

case Some(model) => calcUI(model, centerpane.getWidth, centerpane.getHeight, 5, MAX_LEVEL)

don't correspond to any defined calcUI method. For example for the following definition

def calcUI(model: Model, availableWidth: Double, availableHeight: Double, maxLevel: Int): Option[Group]

you have one more argument than needed.

Lomig Mégard
  • 1,828
  • 14
  • 18
  • 1
    Oh my god, that is extremly embarrassing. You are right, the most clear obvious problem I did not see. Shame on me. Thanks for counting right for me... – Rob Mar 14 '14 at 08:54
  • 2
    Embarrassing, but also a useful data point on why overloading is best avoided. Even for obvious cases, people wind up confused. – som-snytt Mar 14 '14 at 08:58
  • It can happen to the best of us. And I kind of agree with @som-snytt about overloading. You can accept the answer to mark the question resolved. – Lomig Mégard Mar 14 '14 at 08:58
  • This is a great answer because I faced the same issue and only counted after reading the answer! – Hrishi Jan 26 '17 at 22:08