Scala trait syntax

后端 未结 1 2125
梦谈多话
梦谈多话 2020-12-19 22:10

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

package org.stairwaybook.scells
    trait Arithmetic { this: Ev         


        
相关标签:
1条回答
  • 2020-12-19 22:15

    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.

    0 讨论(0)
提交回复
热议问题