【问题标题】:dart mixin 'with' cannot be used without 'extends'?dart mixin 'with' 不能在没有 'extends' 的情况下使用?
【发布时间】:2013-06-20 08:31:50
【问题描述】:

我正在使用 Webstorm 6.0.2 并在尝试使用 mixin 语法时遇到错误:

class A{}

class B with A{} //error can't use with syntax without an extends?

为什么我不能在没有extends 的情况下使用with?当然每个类都隐含extends Object

【问题讨论】:

    标签: syntax dart mixins


    【解决方案1】:

    这里是a really clear explanation from Ladislav Thon

    [...] 有一个简单的建议,实际上在语义上是正确的:在声明 class C extends SC with M1, M2, M3 implements I1, I2 { ... } 中,想象一下extends 子句内容的括号。它们看起来像这样:class C extends (SC with M1, M2, M3) implements I1, I2 { ... }。这意味着 C 类没有扩展 SC,它扩展了 SC_with_M1_with_M2_with_M3。

    或者,换一种说法:类声明有一个extends子句和一个implements子句,但它没有有一个with 子句。相反,with 子句属于 extends 子句。

    还有another point from Florian Loitsch

    当你用 mixin 扩展“Object”时,第一个 mixin 总是可以代替“Object”。

    所以你的class B with A 应该是class B extends Object with A,也相当于class B extends A

    【讨论】:

    • 当它说 mixins 必须从对象扩展时,这是否意味着(在原始问题示例中)A 必须扩展对象或 B 必须扩展对象,或两者兼而有之?我猜我要问的是哪个被认为是 mixin A 或 B 我认为它是 A
    • 请参阅 dartlang.org/articles/mixins/#syntax-and-semantics 了解对 A 的限制。
    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 2021-08-10
    • 2018-06-01
    • 2015-02-04
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    相关资源
    最近更新 更多