在特征内扩展特征时,“超级"指的是什么? [英] When extending a trait within a trait, what does 'super' refer to?

查看:75
本文介绍了在特征内扩展特征时,“超级"指的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特征中扩展特征,像这样:

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

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

  trait ScrumptiousTypes extends NodeTypes {
    trait Node extends super.Node {
      def scrumptiousness: Int
    }
  }

  trait YummyTypes extends NodeTypes {
    trait Node extends super.Node {
      def yumminess: Int
    }
  }

  object Graph extends NodeTypes with ScrumptiousTypes with YummyTypes {
    case class Node() extends super.Node {
      override def allNodesHaveThis = 1
      override def scrumptiousness = 2  // error: ScrumptiousTypes.Node has been disinherited
      override def yumminess = 3
    }
  }

如果这行得通,那将是一个很好的说法:当您的Graph继承自<Whatever>Types时,其Node类必须提供<Whatever>所需的方法."

If this works, it would be a nice way of saying "When your Graph inherits from <Whatever>Types, its Node class must provide the methods required by <Whatever>."

但是Scala 2.11.2编译器说:

But the Scala 2.11.2 compiler says:

error: method scrumptiousness overrides nothing
      override def scrumptiousness = 2
                   ^

似乎YummyTypes.Node遮盖了ScrumptiousTypes.Node,遵循Scala解决方法的钻石"继承的通常方法:通过线性化类型.据我了解,应该是可以的,因为YummyTypes.Node显式扩展了super.Node,对于相同类型的线性化,应引用ScrumptiousTypes.

It appears that YummyTypes.Node shadows ScrumptiousTypes.Node, following the usual way that Scala resolves "diamond" inheritance for methods: by type linearization. As I understand things, that should be OK, though, because YummyTypes.Node explicitly extends super.Node, which, by the same type linearization, should refer to ScrumptiousTypes.

我误解了什么?或者,super.Node是指什么?为什么?

What have I misunderstood? Or, what does super.Node refer to—and why?

如果您想知道为什么要执行此操作,那么就可以立即将更改混合到多个特征中,这样继承的特征就可以互操作,如scrumptiousness成员,而不必列出所有Node-extensions:trait Hypernode extends ScrumptiousTypes.Nodetrait ZealousNode extends ScrumptiousTypes.Node

If you're wondering why I'm doing this, it's so I can mix changes into several traits at once, so the inherited traits interoperate, as explained in this question. In the final Node class (and other classes that it works with), I don't want to explicitly extend from each Node trait: I want to mix in from one "thing" (whatever it is) and get all the mutually consistent changes made to Node and the other traits, all in a bundle. Or, if one trait defines a bunch of extensions to Node, extending from ScrumptiousTypes should make all of the Node-extensions contain a scrumptiousness member, without having to list all the Node-extensions: trait Hypernode extends ScrumptiousTypes.Node, trait ZealousNode extends ScrumptiousTypes.Node, etc.

推荐答案

使用类型也可以解决此问题

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 ------- 使用对象包含到Node, 但是由于依赖路径并不是一个好主意, 也许是问题

------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
  }

}

这篇关于在特征内扩展特征时,“超级"指的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆