Type Definition => No Constructor

EDIT: In the example below I created an incorrect inner constructor. But the part of my question that remains of interest is why is an inner constructor needed at all? What are the features of this type definition that require this?

I ran into a problem today with defining a new type in a more complex way than I normally do and this led to no constructor being defined. I tried to specify my own inner constructor, but without success. Is this a limitation I’m unaware of or am I doing something wrong? (I can work around this but it feels like a hack.)

module M
   abstract type A1{T}  end
   abstract type A2{T}  end
   struct B{S, T} <: A1{T} where {S <: A2{T}}
      x::S
     #  new(x::S) where {S <: A2{T}} where {T}  = B{S, T}(x)
     # EDIT: correct constructor: 
     # B(x::S) where {S <: A2{T}} where {T}  = new{S,T}(x)
   end
   struct C{T} <: A2{T}
      x::T
   end
end

methods(M.B)

The result of methods(...) is the same with or without the inner constructor.

Your inner constructor isn’t quite right, and I’m surprised it doesn’t throw an error. You use new() inside an inner constructor, not as its name.

For example:

struct A{T}
  ...
  A{T}(...) where {T} = new{T}(...)
  ...
end
2 Likes

Ah - that is a bit embarrassing, thanks for pointing that out.

It still leaves the question: why is an inner constructor needed at all?

The type definition syntax seems off. This works as expected:

abstract type A1{T}  end
abstract type A2{T}  end
struct B{T, S <: A2{T}} <: A1{T}
    x::S
end
struct C{T} <: A2{T}
    x::T
end

B(C(1))

brilliant - thank you!