Use function on type parameter to decide struct field type at compile time

There’s no way to do this the way you’ve specified it, unfortunately. Generally you’d work around this by adding another type parameter, like:

struct Foo{T, V}
  a::V
  
  Foo{T}(a) where {T} = new{T, map_type(T)}(a)
end

where the inner constructor is responsible for computing V. The ComputedFieldTypes package has a couple of macros to make that pattern a little bit easier.

1 Like