-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'Flatten' state objects #234
Comments
It's worth noting that in 0.3, it's less cumbersome to do thanks to #35. You can (finitely) unwrap something using nested local parcel = Value(Value(Value("toothbrush")))
local unwrapped = Computed(function(use)
-- unwraps up to five layers
return use(use(use(use(use(parcel)))))
end)
print(peek(unwrapped)) --> toothbrush You could perhaps unwrap an indefinite number of layers using a loop; this is definitely not ergonomic though. |
Relates to #142 |
Ah yes. I think it'll be worth addressing the points I made previously there. For clarification, that previous issue related to implicit flattening in
I still maintain that changing use/peek to flatten state objects would be far too heavy handed. I think it's about 50% true for
I think this is true, and another reason why explicit flattening works better.
Still true. Explicit flattening removes the intent problem. This is why I'm iffy about
Explicit flattening works with all state objects by default.
Explicit flattening makes this a non-issue, as introducing explicit flattening does not change existing behaviour. |
Design should be simple enough for this. Either we could introduce a |
This can be trivially implemented as a function; local function flatten<T>(
use: Use,
thing: CanBeState<T>
): T
while isState(thing) do
thing = use(thing)
end
return thing
end I suggest we go for this function implementation first, and only create a full state object if it becomes a common pattern. |
Upgrading the importance of this feature request because #301 may increase the frequency of code that needs to deal with an unknown level of state object nesting. |
Right now it can be awkward to work with nested state objects, especially if you don't know how deeply nested they will be, because in order to correctly access the value you need, you also have to manage a lot of observers to make sure you catch when one of the state objects at any level changes value.
We should probably implement this as a new kind of state object, which can take in any existing state object and output the innermost value obtained by unwrapping as many times as possible (or perhaps a configurable number of unwraps).
The text was updated successfully, but these errors were encountered: