gpt4 book ai didi

julia - 是否可以从抽象类型 julia 以外的东西继承?

转载 作者:行者123 更新时间:2023-12-05 01:24:59 26 4
gpt4 key购买 nike

我有一个无法更改的函数,它需要类型 ExpectedType

function some_function(some_parameter::ExpectedType)
....
some implementation
....
end

我想传递类型为 OtherType 的对象。

我决定从 ExpectedType 继承 OtherType

struct OtherType <: ExpectedType end

但我收到一个错误:错误:OtherType 定义中的子类型无效

  1. 是否可以从非抽象类型继承?
  2. 我怎样才能获得我需要的功能?

最佳答案

  1. 不可能从非抽象类型继承。
  2. 我通常会做的是为 ExpectedType 编写一个构造函数,它将 OtherType 作为参数,然后调用 some_function(ExpectedType(your_object)) .

然后你可以定义:

SourceModuleName.some_function(x::OtherType) = some_function(ExpectedType(x))

这样您就不必显式调用构造函数。

关于julia - 是否可以从抽象类型 julia 以外的东西继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71031503/

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