gpt4 book ai didi

scala - 我们真的在 Scala 中拥有 'extend' 特征吗?

转载 作者:行者123 更新时间:2023-12-01 04:49:52 25 4
gpt4 key购买 nike

在学习 Scala 时,我一直在努力理解一个特定的细微差别:
trait Testclass Tester with Test //#A: Throws compilation errorclass Tester extends Test //#B: works just fine
现在,我们并没有真正扩展特征本身(或者我们是?)。以下是我的问题:

  • 扩展特征与扩展类有何不同?它只是单个特征的特殊情况语法,我只是将其作为语言细微差别接受吗?
  • 为什么#A 不正确?为什么我们不能这样做?如果没有任何父类(super class)可以继承,为什么我们必须扩展特征?

  • Martin Odersky 在回复 similar question 时表示这是公约,但人们发现它令人困惑。我不确定为什么会这样,或者是什么问题导致了这个决定?但这是他的回应:

    We had that convention early on in the design of Scala. People found it confusing. That's why we changed to use always `extends'. The way I see it is like this:

    class A extends B with C { ... }

    should be decomposed as:

    class A extends <<< B with C { ... } >>>

    That is, A is a class that extends the anonymous template B with C { ... }. It does not matter whether that template starts with a class or a trait.

    Another advantage of the new convention is that subclasses are not affected when a class is changed to a trait or vice versa, something that happens quite often and naturally.



    尽管我可以接受他的解释并重新思考我的想法(并看到将类更改为特征的优势,仅仅是设计选择的副作用)我仍然希望更直观地理解这种细微差别。

    最佳答案

    我可以给你看一个例子,我觉得“扩展”一个 trait 的“概念”其实是合乎逻辑的

    看看下面的代码混合使用 Structural TypeSelf Referencing Trait ,

    type Fooable = {
    def foo(): Unit
    }


    trait FooableExtra { self: Fooable =>

    def omgWeCanFoo(): Unit = {
    println("foo :: by :: FooableExtra")
    self.foo()
    }

    }

    class OneThingWithFoo extends FooableExtra {

    def foo(): Unit = {
    println("foo :: by :: OneThingWithFoo")
    }

    def oneThing: Unit = {}
    }

    我无法用语言来解释它,它更像是一种直观的东西......但这就是让我在写 class A extends TraitA 之类的东西时问心无愧的原因。 .

    关于scala - 我们真的在 Scala 中拥有 'extend' 特征吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42308847/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com