Julia类型/继承

huangapple go评论54阅读模式
英文:

Julia Typing/Inheritance

问题

Note: 我找到了这个帖子,并阅读了Julia的文档的这一部分,但我还没有弄清楚。因此,我写了这篇帖子。

假设我们有以下两个结构体:

struct A
    x
end

struct B
    x
    y
end

其中AB的泛化。因此,在Python或C++等其他语言中,我会让B继承自A。不幸的是,在Julia中,根据我目前的理解,没有继承的概念。如果我理解错了,请纠正我!

我对Julia中抽象类型和子类型的理解,到目前为止:

我刚刚了解了Julia中抽象类型和子类型的概念。到目前为止,我明白我可以这样做:

abstract type C end

struct A <: C
    x
end

struct B <: C
    x
    y
end

因此,AB都是C的子类型,以下等式成立:

(A <: C) == true
(B <: C) == true
(A <: B) == false
(B <: A) == false

因此,我不能将B的实例分配给类型为A的变量,反之亦然。

问题:

考虑第一个代码示例。是否有可能使B“继承”自A,而不必再次定义B中的字段x?就像我从其他语言的继承中期望的那样。

希望我的问题清楚,否则,请提出问题!提前感谢任何提示/评论! Julia类型/继承

英文:

Note: I found this post and read this part of Julia's documentation, but I couldn't figure it out yet. Therefore, I wrote this post.

Suppose we have the following two structs:

struct A
    x
end

struct B
    x
    y
end

whereby A is a generalization of B. Therefore, in other languages like Python or C++ I would make B inherit from A. Unfortunately, in Julia there is no inheritance, from my understanding so far. Please correct me if I'm wrong!

My Understanding About Abstract Types & Sybtyping in Julia, so far:

I just read about the concept of abstract types and subtyping in Julia. So far I've understood that I could do the following:

abstract type C end

struct A &lt;: C
    x
end

struct B &lt;: C
    x
    y
end

Hence, A and B are subtypes of C and the following holds:

(A &lt;: C) == true
(B &lt;: C) == true
(A &lt;: B) == false
(B &lt;: A) == false

Thus, I can't assign an instance of B to a variable of type A and vise versa.

Question:

Consider the first code example. Is it possible to make B "inherit" from A and to not define the field x in B again? Like I'd expect it from inheritance in other languages.

I hope my problem/question is clear, otherwise, please ask! Thanks for any hint/comment in advance! Julia类型/继承

答案1

得分: 4

像我从其他语言的继承中预期的那样。

不,这是不可能的。此外,这是一个有意的设计选择。

通常,在Julia中的设计遵循组合优于继承的原则。

因此,如果你确实想在B中使用A,通常会这样做:

struct B
    a::A
    y
end

另一种选择是使用你定义的抽象上限类型:

abstract type C end

struct A <: C
    x
end

struct B <: C
    x
    y
end

并为C定义一个接口,要求所有它的子类型定义x字段,例如:

getx(::C) = throw(ArgumentError("C的子类型必须定义x的getter"))
getx(a::A) = a.x
getx(b::B) = b.x

在你的简单情况下,你也可以什么都不定义,因为以下代码将正常工作(只要C的所有子类型都定义了x属性 - 这应该在C的文档中明确说明是必需的):

f(c::C) = typeof(c.x)
英文:

> Like I'd expect it from inheritance in other languages.

No, it is not possible. Additionally, it is an intended design choice.

In general in Julia the design follows Composition over inheritance principle.

Therefore if you indeed want to use A in B then typically you would do:

struct B
    a::A
    y
end

An alternative is to use an abstract upper type as you defined it:

abstract type C end

struct A &lt;: C
    x
end

struct B &lt;: C
    x
    y
end

And define an interface for C that requires all its subtypes to define x field, eg. like this:

getx(::C) = throw(ArgumentError(&quot;subtypes of C must define getter of x&quot;))
getx(a::A) = a.x
getx(b::b) = b.x

In your simple case you also could just leave out defining anything, as the following code:

f(c::C) = typeof(c.x)

will work just fine (as long as all subtypes of C define property x - which should be clearly stated in the documentation of C that it is required).

huangapple
  • 本文由 发表于 2023年6月1日 02:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定