我有一个无法更改的函数,并且它需要 type ExpectedType。
function some_function(some_parameter::ExpectedType)
....
some implementation
....
end
Run Code Online (Sandbox Code Playgroud)
我想传递我的 type 对象OtherType。
我决定OtherType继承ExpectedType.
struct OtherType <: ExpectedType end
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:ERROR: invalid subtyping in definition of OtherType
ExpectedType参数OtherType,然后调用some_function(ExpectedType(your_object))。然后你也可以定义:
SourceModuleName.some_function(x::OtherType) = some_function(ExpectedType(x))
Run Code Online (Sandbox Code Playgroud)
这样您就不必显式调用构造函数。