Mixins 与 Traits

发布于 2024-07-22 15:25:53 字数 155 浏览 5 评论 0原文

Mixin 和 Traits 有什么区别?

根据 Wikipedia,Ruby 模块有点像特征。 为何如此?

What is the difference between Mixins and Traits?

According to Wikipedia, Ruby Modules are sort of like traits. How so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2024-07-29 15:25:53
  1. Mixin 可能包含状态,(传统)特征则不包含。
  2. Mixins 使用“隐式冲突解决”,traits 使用“显式冲突解决”
  3. Mixins 依赖于线性化,traits 被扁平化。

关于特质的讲座

广告 1.
在 mixin 中你可以定义实例变量。 特质不允许这样做。 状态必须由组合类(=使用特征的类)

ad 2. 提供
可能存在名称冲突。 两个 mixin(MAMB)或特征(TATB)定义具有相同定义的方法 <代码>foo():void。

Mixin MA {
    foo():void {
        print 'hello'
    }
}

Mixin MB {
    foo():void {
        print 'bye'
    }
}

Trait TA {
    foo():void {
        print 'hello'
    }
}

Trait TB {
    foo():void {
        print 'bye'
    }
}

在 mixins 中,组合类 C mixins MA、MB 中的冲突会被隐式解决。

Class C mixins MA, MB {
    bar():void {
        foo();
    }
}

这将从 MA 调用 foo():void

另一方面,在使用 Traits 时,组合类必须解决冲突。

Class C mixins TA, TB {
    bar():void {
        foo();
    }
}

此代码会引发冲突(foo():void 的两个定义)。

广告 3。
方法的语义并不取决于它是在特征中定义还是在使用该特征的类中定义。

换句话说,类是否由 Traits 组成或 Traits 代码是否“复制粘贴”到类中并不重要。

  1. Mixins may contain state, (traditional) traits don't.
  2. Mixins use "implicit conflict resolution", traits use "explicit conflict resolution"
  3. Mixins depends on linearization, traits are flattened.

Lecture about traits

ad 1.
In mixins you can define instance variables. Traits do not allow this. The state must be provided by the composing class (=class using the traits)

ad 2.
There may be the name conflict. Two mixins (MA and MB) or traits (TA and TB) define the method with the same definition foo():void.

Mixin MA {
    foo():void {
        print 'hello'
    }
}

Mixin MB {
    foo():void {
        print 'bye'
    }
}

Trait TA {
    foo():void {
        print 'hello'
    }
}

Trait TB {
    foo():void {
        print 'bye'
    }
}

In mixins the conflicts in composing class C mixins MA, MB are resolved implicitly.

Class C mixins MA, MB {
    bar():void {
        foo();
    }
}

This will call foo():void from MA

On the other hand while using Traits, composing class has to resolve conflicts.

Class C mixins TA, TB {
    bar():void {
        foo();
    }
}

This code will raise conflict (two definitions of foo():void).

ad 3.
The semantics of a method does not depend of whether it is defined in a trait or in a class that uses the trait.

In other words, it does not matter whether the class consists of the Traits or the Traits code is "copy - pasted" into the class.

永言不败 2024-07-29 15:25:53

这些页面解释了 D 编程语言的差异。

http://dlang.org/mixin.html

http://dlang.org/traits.html

在此上下文中的 Mixin 是动态生成的代码,然后在编译期间插入到代码中的该点。 对于简单的 DSL 来说非常方便。

特征是编译时外部值(而不是从外部源生成的代码)。 差异是微妙的。 Mixins 添加逻辑,Traits 添加数据,例如编译时类型信息。

对Ruby不太了解,但希望对您有所帮助。

These pages explain the difference in the D Programming language.

http://dlang.org/mixin.html

http://dlang.org/traits.html

Mixins in this context are code generated on the fly, and then inserted at that point in code during compilation. Quite handy for simple DSLs.

Traits are compile-time external values (rather than code generated from an external source). The difference is subtle. Mixins add logic, Traits add data such as compile-time type information.

Don't know much about Ruby, but hope this helps somewhat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文