When extending a trait within a trait, what does 'super' refer to?

后端 未结 3 2132
忘掉有多难
忘掉有多难 2021-02-19 05:34

I want to to extend a trait within a trait, like this:

  trait NodeTypes {
    trait Node {
      def allNodesHaveThis: Int
    }
  }

  trait ScrumptiousTypes e         


        
3条回答
  •  日久生厌
    2021-02-19 05:38

    use type also fix the issue

    trait NodeTypes {
    
      trait Node {
        def allNodesHaveThis: Int
      }
    
    }
    
    trait ScrumptiousTypes extends NodeTypes {
    
      trait Node extends super.Node {
        def scrumptiousness: Int
      }
    
      type ScrumptiousTypesNode = this.Node
    }
    
    trait YummyTypes extends NodeTypes {
    
      trait Node extends super.Node {
        def yumminess: Int
      }
    
      type YummyTypesNode = this.Node
    }
    
    object Graph extends NodeTypes with ScrumptiousTypes with YummyTypes {
    
      case class Node() extends ScrumptiousTypesNode with YummyTypesNode {
        override def allNodesHaveThis = 1
    
        override def scrumptiousness = 2
    
        override def yumminess = 3
      }
    
    }
    

    ------v2------- use object contain to Node , but since path depend it is not a good idea , and maybe It will be problems

    trait NodeTypes {
    
      trait Node {
        def allNodesHaveThis: Int
      }
    
    }
    
    object NodeTypes extends NodeTypes
    
    trait ScrumptiousTypes extends NodeTypes {
    
      trait Node {
        def scrumptiousness: Int
      }
    
      type ScrumptiousTypesNode = this.Node
    }
    
    object ScrumptiousTypes extends ScrumptiousTypes
    
    trait YummyTypes extends NodeTypes {
    
      trait Node {
        def yumminess: Int
      }
    
      type YummyTypesNode = this.Node
    }
    
    object YummyTypes extends YummyTypes
    
    trait Nodes {
    
      trait Nodes extends NodeTypes.Node with  YummyTypes.Node with ScrumptiousTypes.Node
    
    }
    
    
    object Graph extends  Nodes {
    
      case class Nodes() extends super.Nodes {
        override def yumminess: Int = 1
    //
        override def scrumptiousness: Int = 2
    
        override def allNodesHaveThis: Int = 3
      }
    
    }
    

提交回复
热议问题