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

Parametrized struct

Open sivark opened this issue 2 years ago • 2 comments

Hi, I observe the following behavior with @proto while the same code works in standard Julia (without the @proto). Is this a bug, or is it out of scope for the current design/implementation?

For context, the code below is trying to create a struct for d-dim points, to hold the point coordinates.

julia> @proto struct  pt{d}
           coords::NTuple{d,Int64}
       end


julia> pt{2}((1,1))
ERROR: TypeError: in pt, in NT, expected NT<:NamedTuple, got a value of type Int64
Stacktrace:
 [1] top-level scope
   @ REPL[99]:1

julia>

PS: Thanks for a very useful package :-)

sivark avatar Apr 07 '22 15:04 sivark

Currently this package creates the constructor

pt(coords::NTuple{d, Int64}) where d

So you can use pt((1,1)).

It should be possible to extend the package to also define the constructor

pt{d}(coords::NTuple{d, Int64}) where d

though.

BeastyBlacksmith avatar Aug 18 '22 09:08 BeastyBlacksmith

Its trickier than I thought. The issue is, that the backing struct already has a typeparameter NT. So it tries calling that constructor. We could just name the structure differently, but then you loose the @proto struct Struct end; Struct() isa Struct property. I tried adding the type parameter to the backing struct, but those constructors don't work the way I want them to work.

So maybe it can be solved with an inner constructor.

BeastyBlacksmith avatar Aug 19 '22 08:08 BeastyBlacksmith