Scala trait syntax

2020-02-13 01:29发布

问题:

I am reading Odersky's book and there is a speadsheet example with the follwowing code part:

package org.stairwaybook.scells
    trait Arithmetic { this: Evaluator =>
      operations += (
        "add"  -> { case List(x, y) => x + y },
        "sub"  -> { case List(x, y) => x - y },
        "div"  -> { case List(x, y) => x / y },
        "mul"  -> { case List(x, y) => x * y },
        "mod"  -> { case List(x, y) => x % y },
        "sum"  -> { xs => (0.0 /: xs)(_ + _) },
        "prod" -> { xs => (1.0 /: xs)(_ * _) }
      )
    }

what does "this: Evaluator" refer to? Could someone help to understand this trait? As I see it defines different operations which are functions, but I do not see the big picture...

回答1:

What you see here

this:Evaluator =>

is the usage of self-type for a trait. It basically forces class which are going to mix the trait Arithmetic to also mix the trait Evaluator.

If you try to create a class such as the following:

class ArithmeticClass extends Arithmetic

you'll get a compile time error, while if you try to do:

class ArithmeticClass extends Arithmetic with Evaluator

this is going to work. As you can see, Arithmetic class modifies add something to operations, which is probably a collection defined in the Evaluator trait.

Please note that self-types let you design a cleaner class hierarchy compared to simple inheritance:

If you use self types, you can think about something like the following:

trait Evaluator { def runEvaluation : Int }
trait Arithmetic { self: Evaluator => def evaluate: Int = runEvaluation }
trait NullEvaluator extends Evaluator { def runEvaluation: Int = 0 }

class MyClass1 extends Arithmetic with Evaluator {... concrete methods .... }
class MyClass2 extends Arithmetic with NullEvaluator { ....concrete methods ..... }

So self types let you express something different from inheritance.



标签: scala