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

parent type copy constructors

Open mhinsch opened this issue 1 year ago • 5 comments

This is a quick proof of concept for issue #7. As far as I can tell it works fine, but I haven't tested any edge cases. No documentation or testing yet.

mhinsch avatar Jun 08 '23 08:06 mhinsch

Just took a look at it, this idea makes a lot of sense to me, thanks! If you can add tests and a note in the readme I'll merge.

marius311 avatar Jun 10 '23 21:06 marius311

This seems to break the readme example fyi, which you may have seen and I'm sure is fixable.

marius311 avatar Jun 10 '23 21:06 marius311

Ok, I've fixed the basic issue with parametric parent types (which was actually a bit stupid). The two different constructor generation parts could be combined and simplified a bit, but I left them separate for now.

I'm now running into a strange error in line 118 of runtests.jl, will keep investigating.

mhinsch avatar Jun 11 '23 10:06 mhinsch

@marius311 the problem seems to be that now that the macro always returns a struct plus the new "copy constructor", outer @kwdef breaks (as it expects just a struct as input). As far as I can see the only way to fix this would be to only generate the constructor in the (inner) kwdef case which doesn't make a lot of sense. So, I think it boils down to choosing one of two functionalities - outer kwdef or copy constructors. Personally, I would ditch outer kwdef, but it's your package.

mhinsch avatar Jun 11 '23 14:06 mhinsch

Hmm, this got me thinking about the wider conceptual issues with my PR. So far @composite is strictly orthogonal to struct definition. The output of the standard version behaves exactly as you would expect from a plain struct declaration. In particular which constructors are or aren't generated follows exactly the same rules irrespective of whether you use @composite or not (with or without @kwdef). With my PR this is no longer the case. For example, if a user defines an inner constructor a) there will still be an outer constructor (contrary to expectations) and b) the "copy constructor" might simply break if the inner constructor has a different argument list than the default constructor. All of this is true for Base @kwdef as well, of course, so one might argue that this is not a problem, but at the very least it is a design decision to be made.

mhinsch avatar Jun 11 '23 15:06 mhinsch