使用 self 类型扩展 trait 时非法继承 [英] Illegal inheritance while extending trait with self type

查看:43
本文介绍了使用 self 类型扩展 trait 时非法继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码感到困惑:

I'm confused by the following code:

    trait T1 {
        type S
    }

    trait T2 {
      self: T1 =>

       case  class ClS(s: S)
    }

    object O extends T2 {

    }

它给出了以下错误消息:

It gives out the following error message:

非法继承;tests.O.type 不符合 tests.T2 的selftype tests.T2 with tests.T1

illegal inheritance; tests.O.type does not conform to tests.T2's selftype tests.T2 with tests.T1

怎么了?我用 T2 扩展 O 的原因是我想引用 O 中的 CLS 类,怎么能我做到了?

What's going wrong? the reason that I extend O with T2 is that I want to reference the class CLS in O, how can I achieve that?

推荐答案

trait T2 有个约定说:如果你继承我,你也应该继承T1.

trait T2 has a contract that says: If you inherit from me, you should also inherit from T1.

object O extends T2 with T1

然后您可能会收到一个错误,指出 type S 未定义.这是一件好事,因为它用于 CLS

You would then probably get an error that type S is not defined. Which is a good thing since it's used in the definition of CLS

这篇关于使用 self 类型扩展 trait 时非法继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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