我有一组像这样的案例类
abstract class Shape
case class Rectangle(width: Int, height: Int) extends Shape
case class Location(x: Int, y: Int, shape: Shape) extends Shape
case class Circle(radius: Int) extends Shape
case class Group(shape: Shape*) extends Shape
Run Code Online (Sandbox Code Playgroud)
其中基本上Group是一个形状数组.我需要定义一个大小方法来计算矩形,圆形和位置的大小,它直接只返回一个.但我对集团有困难.
object size extends Shape{
def size(s: Any) : Int = s match {
case Rectangle(x,y) => 1
case Group // how to do it? Also having case Group(shape : Shape*) gives an error
case Circle(r) => 1
case Location(x,y,shape) => 1
}
}
Run Code Online (Sandbox Code Playgroud)
我知道对于Group我需要使用map并向左折叠,但我真的无法为它创建逻辑.谢谢