I have been working on a project in scala, but I am getting some error messages that I don t quite understand. The classes that I am working with are relatively simple. For example:
abstract class Shape
case class Point(x: Int, y: Int) extends Shape
case class Polygon(points: Point*) extends Shape
我现在假定我会创造一个多功能:
val poly = new Polygon(new Point(2,5), new Point(7,0), new Point(3,1))
然后,如果我试图确定可能包含多角的最小 rec的位置和大小,我就会发现我不理解的各种错误。
下面是不同尝试的幻灯和它们产生的相应的错误信息。
val upperLeftX = poly.points.reduceLeft(Math.min(_.x, _.x))
Gives the error:
"missing parameter type for expanded function ((x$1) => x$1.x)"
val upperLeftX =
poly.points.reduceLeft((a: Point, b: Point) => (Math.min(a.x, b.x)))
Gives this error:
"type mismatch;
found : (Point, Point) => Int
required: (Any, Point) => Any"
我非常混淆了这两种错误信息。 如果任何人能够更清楚地解释我不正确做些什么,我会真的赞赏。 是的,我认为第二个错误说,我需要“阿尼”类,但我不确切理解如何实施我所需要的变革。 显然,仅仅将“a:点”改为“a:任何”不是一个可行的解决办法,因此我失踪了什么?