ampersand-state icon indicating copy to clipboard operation
ampersand-state copied to clipboard

I should be able to modify State's getters and setters for an individual attribute

Open pixelpax opened this issue 8 years ago • 4 comments

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.

pixelpax avatar Jan 28 '16 15:01 pixelpax

I'll start working on this actually, it doesn't look too difficult.

pixelpax avatar Jan 28 '16 15:01 pixelpax

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?

wraithgar avatar Jan 28 '16 16:01 wraithgar

In my opinion (which may be uninformed) there are two reasons why this solution isn't enough:

  1. 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.
  2. 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).

pixelpax avatar Jan 28 '16 18:01 pixelpax

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.

wraithgar avatar Jan 28 '16 18:01 wraithgar