jdeans289 / icg2

Interface Code Generator 2: Electric Boogaloo
Other
0 stars 2 forks source link

Idea - rework for PrimitiveDataType #11

Closed jdeans289 closed 1 year ago

jdeans289 commented 1 year ago

The PrimitiveDataType causes problems sometimes because we're trying to mix dynamic polymorphism and templates. The structure of what we are trying to represent and the algorithms that we need to perform over them work so well because it is a very natural fit for an inheritance hierarchy, and the Visitors enable us to write whatever algorithm over the tree without having to expand the class or resort to runtime casting. The PrimitiveType throws a wrench in the Visitors because C++ does not allow a virtual templated function. For now, the workaround is just to take a DataType* in the visitPrimitiveType interface, which is a bit unfortunate, but is ok as long as we can get everything we need out of the base DataType interface. I could definitely see this coming back to bite me later though. I really want to keep the DataType interface as small as possible and move everything other than the most basic operations to Visitors, but the template parameter for PrimitiveDataType was gumming that up.

I think the solution is to create an un-templated PrimitiveDataType abstract class that the current PrimitiveDataType can inherit from. That way we can take this in a Visitor and and add any specialized primitive interface we need there.

I'm a little wary of adding too many layers in our inheritance hierarchy, but this seems like a good use of it to me. We want to hide the template parameter, but still have the interface. I tried looking into type erasure for this, but I'm not sure that I really understand it or if it's applicable here.

This would let us implement assignValue, getValue, printValue etc methods as Visitors, and break the dependency between the Type and Value hierarchy, which is tenuous anyway. It would shrink the DataTypes interface.

I'm a really big fan of the Visitors because it allows us to implement the entirety of one "algorithm" in one place, instead of having to spread it across all of the different DataType classes. This also prevents the DataType classes from bloating.

jdeans289 commented 1 year ago

Also if I do this it should be soon, adjusting all the Visitors and tests will be annoying.