New to scala and trying to get the hang of the class system. Here's a simple set up:
sealed trait Shape{
def sides:Int
}
final case class Square() extends Shape {
def sides() = 4
}
final case class Triangle() extends Shape {
def sides() = 3
}
Now, I want to create a function that takes anything of type shape
, which we know will have a sides()
method implemented, and make use of that method.
def someFunction(a: Shape)={
val aShape = a()
aShape.sides()
}
But this hits an error at val aShape = a()
, as there's no type a
.
I realize that in this example, it's excessive to create someFunction
, since sides()
can be accessed directly from the objects. But my primary question is in the context of someFunction
- I'd like to pass a class to a function, and instantiate an object of that class and then do something with that object. Thanks for your help.