ampersand-state
ampersand-state copied to clipboard
I should be able to modify State's getters and setters for an individual attribute
I want to be able to follow some procedure every time an attribute is modified. For example if I wanted a vector object whose x and y components should be interdependent with its polar components, magnitude and direction, I cannot make all four of these numbers derived objects because their interdependence becomes circular. What I would like to do instead is initialize the 'x' prop with a 'set' function, to automatically update the polar coordinates, etc.
Ampersand-State allows you to specify the 'set' behavior for a dataType as a whole, but this only works if I want to modify all 'number' values to have the same setting behavior.
If there is a more trivial way to accomplish what I'm describing, I would be interested.
I'll start working on this actually, it doesn't look too difficult.
Historically people have set up custom data types for cases like this, and is just the sort of problem that was trying to be solved when custom data types were added.
Have you tried that? Or is there some reason why a attribute-level override would be better?
In my opinion (which may be uninformed) there are two reasons why this solution isn't enough:
- Because sometimes one wants to treat different attributes with the same datatype differently. Take for example the scenario with a vector type that has dual representations. We want to change what happens 'x' and 'mag' are set, but in different ways. Since both are numbers, we can't change one's set method without changing both.
- I imagine it would be common to want to change the 'set' behavior on a primitive attribute. Users should be able to do this as simply as possible, by adding a 'set' method onto the prop. To do so right now requires that one create a an entirely new and disjointed sub-object within the state to specify the behavior of the custom dataType. They shouldn't have to rewrite the compare function or worry about casting, since these are already taken care of by default behavior. It does work-- if one only has one primative they want to customize like this --but it seem unintuitive and little verbose (from the perspective of someone who is just beginning to use Ampersand like myself).
Your opinion was in fact very informed, and from a very valuable perspective. Please do consider submitting a PR for this. I for one would look forward to it.