Intent to change: Multiple returns from Computed #189
dphfox
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Starting with Fusion v0.2, you may see this warning in your output.
This warning will be triggered whenever your code returns more than one value from a Computed callback:
How do you return multiple values?
In Lua, it is possible for functions to return multiple separate values, by providing them as a comma-separated list after the
return
keyword. The multiple values can then be captured by assigning to multiple variables at the same time:It's important to note; this is not the same as returning a table of objects. Tables are single values - they just contain multiple other values inside of themselves, in the form of keys and values.
This post does not refer to returning tables of values. It only refers to the first example, where multiple values are being returned outside of a table.
How do Computeds handle multiple returns right now?
Right now, Computeds adopt the first value you return from your callback, and discards all other returned values.
What's going to change?
As part of ongoing work to introduce better memory safety into Fusion, Computeds will soon be able to define custom destruction code to clean up previously generated values when they are no longer being used. More information about destructors is provided in this other discussion.
The existing
ComputedPairs
(v0.1) andForKeys
/ForValues
/ForPairs
(v0.2) objects already implement destructors. With these changes, we intend to bring the same APIs toComputed
.Those existing objects allow you to pass extra data to the destructor by including it after the returned values:
In the future, we intend to bring this behaviour to Computeds too:
In addition, Fusion's default
cleanup
destructor will accept multiple arguments:Why could this cause a problem?
We want to make sure that we don't accidentally obscurely break people's code. It's unlikely - but entirely possible - that some code may depend on Computed implicitly discarding extra returned values. For example:
As we implement destructors, we will be encouraging developers to annotate their Computeds with
Fusion.cleanup
when destruction behaviour is needed. However, this will change how extra returned values are handled - the extra values may be destroyed instead of being discarded, which could break some code.What are we doing about it?
To alert people who may be at risk of unintended behaviour from these potential changes, we will not pass extra values to the destructor for Computeds for the time being. Instead, we will warn you when you return multiple values. This should give you time to find and adjust any problematic code snippets that may be affected as part of these changes in the future.
This should not generally affect most people though - this is mostly a proactive measure against rare issues.
If you have any questions, feel free to reach out!
Beta Was this translation helpful? Give feedback.
All reactions