Parameters.jl icon indicating copy to clipboard operation
Parameters.jl copied to clipboard

Automatically parameterize types

Open greimel opened this issue 7 years ago • 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.

greimel avatar Oct 08 '18 19:10 greimel

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.

Datseris avatar Oct 08 '18 22:10 Datseris

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.

mohamed82008 avatar Oct 09 '18 00:10 mohamed82008

I think the syntax ought to be

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

to not break existing code.

mauro3 avatar Nov 23 '19 15:11 mauro3