mauro3/Parameters.jl

Automatically parameterize types

greimel opened this issue · 3 comments

On discourse, I asked for a way to automatically parameterize types with a macro.

@parameterize struct Foo
   field1::Number
   field2::AbstractVector
   field3
end

yields

struct Foo{T1 <: Number, T2 <: AbstractVector, T3}
   field1::T1
   field2::T2
   field3::T3
end

@mohamed82008 provided the solution for this. It was suggested to add such functionality to this package. Potentially playing out nicely with @with_kw.

In the discourse post it was mentioned that this functionality exists in QuickTypes. However, I don't think that matters. I think it is a great idea to add it to Parameters. There should be one great package that has all the good features.

My solution has some unhandled corner cases, mostly related to bad type definitions, like an integer showing up or something like that. Should be easy to fix though. Also inner constructors are not supported and not sure how well it can play with @with_kw since I haven't read the latter. So it will probably be more than a copy-paste PR.

I think the syntax ought to be

@parameterize struct Foo
   field1<:Number
   field2<:AbstractVector
   field3<:Any
end

to not break existing code.