mwchase / python-structured-data

MIT License
2 stars 0 forks source link

namedtuple and NamedTuple replacement that takes mixins to prefix #20

Closed mwchase closed 5 years ago

mwchase commented 5 years ago

Or maybe, some kind of helper metaclass that could be applied to mixins intended to go ahead of NamedTuples/namedtuples.

Motivation:

class PendulumStateImpl(typing.NamedTuple):

    theta: np.double = np.double(np.pi / 3)
    theta_dot: np.double = np.double(0)

class PendulumState(ScalableNamedTuple, PendulumStateImpl):
    pass
mwchase commented 5 years ago

Piggyback off the existing implementation by actually constructing the namedtuple, but with an empty ns, then subtype it with the actual bases, substituting the namedtuple (require NamedTuple to be at the end of the bases), but forward the

I suddenly remember why I don't usually do this exact kind of thing.

mwchase commented 5 years ago

From inspecting Dennis's use of the data keyword, this is top priority.

Current idea is to investigate making a class decorator that generates a new class. Basically, aim to re-implement namedtuples, but using ADT design principles. May end up renaming the adt function to some derivative of "sum", and this can be "product".

mwchase commented 5 years ago

Wait, this is just frozen dataclasses, isn't it.

mwchase commented 5 years ago

... But, I can hook into the existing destructurers instead of trying to teach it dataclasses.

Current plan is to try making a base class that pulls in the necessary dependencies and does stuff in a __init_subclass__ function. There should be no final-type trickery.

mwchase commented 5 years ago

Possibly, sum types should be made to work by the same mechanism.

mwchase commented 5 years ago

Untested, but this stuff is in the current master.