Alexander Rakhmaev
Alexander Rakhmaev

Reputation: 1055

Is it possible to inherit from something other than the abstract type julia?

I have a function that I can't change and it expects type ExpectedType.

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

I want to pass my object of type OtherType.

I decided to inherit OtherType from ExpectedType.

struct OtherType <: ExpectedType end

But I'm getting an error: ERROR: invalid subtyping in definition of OtherType

  1. Is it possible to inherit from non-abst types?
  2. How can I get the functionality I need?

Upvotes: 3

Views: 260

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69829

  1. It is not possible to inherit from non-abstract type.
  2. What I would typically do is writing a constructor for ExpectedType that takes OtherType as argument and then call some_function(ExpectedType(your_object)).

Also then you could define:

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

so that you do not have to call the constructor explicitly.

Upvotes: 6

Related Questions