Correct use of Scala traits and case objects

Trying to get a view of Scala classes and traits, here's a simple example. I want to define a class that defines a variety of operations that can be implemented in a variety of ways. I could start with

sealed trait Operations{
  def add
  def multiply
}

      

So, for example, I could instantiate this class with an object, which makes add

and is multiply

very sane,

case object CorrectOperations extends Operations{
    def add(a:Double,b:Double)= a+b
    def multiply(a:Double,b:Double)= a*b
}

      

And also there may be other ways of defining these operations, such as obviously the wrong way,

case object SillyOperations extends Operations{
    def add(a:Double,b:Double)= a + b - 10
    def multiply(a:Double,b:Double)= a/b
}

      

I would like to pass such an instance to a function that will perform operations in a specific way.

 def doOperations(a:Double,b:Double, op:operations) = {
   op.multiply(a,b) - op.add(a,b)
 }

      

I would like to doOperations

take any type object operations

that I can use add

and multiply

whatever they are.

What do I need to change about doOperations

, and what am I missing here? Thanks to

+3


source to share


1 answer


Didn't run your code, but I'm guessing you have a compilation error.

If you do not define a method signature in a trait Operations

, then by default it will be interpreted as () => Unit

.

This means that methods of inheriting objects do not actually override methods in a trait, but instead define overloads. More on this here . You can check this by writing override

before method definitions in object methods. This will force the compiler to explicitly warn you that the methods do not override anything from the ancestor trait and act as a "safety net" against such errors.

To correct the error, specify the signature of the attribute as follows:



sealed trait Operations{
  def add(a:Double,b:Double):Double
  def multiply(a:Double,b:Double):Double
}

      

In fact, the types of the out parameters are not even needed in the methods of the inheriting objects (but note the added attributes override

):

case object CorrectOperations extends Operations{
    override def add(a:Double,b:Double) = a+b
    override def multiply(a:Double,b:Double) = a*b
}

      

+5


source







All Articles