Skip to content
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

Chained state objects #414

Open
dphfox opened this issue Jan 24, 2025 · 3 comments
Open

Chained state objects #414

dphfox opened this issue Jan 24, 2025 · 3 comments
Labels
enhancement New feature or request not ready - evaluating Currently gauging feedback

Comments

@dphfox
Copy link
Owner

dphfox commented Jan 24, 2025

Currently, we write a lot of:

scope:Spring(
    scope:Computed(function()
        ...
    end),
    50
)

What if instead, you could write more of:

scope:Computed(function()
    ...
end):Spring(50)

In short, the proposal would be to allow extending the metatable of created state objects to include method calls to state object constructors that declare themselves as chainable.

In the future, this could lead to functional-style pipelines:

numCoins
    :Map(function(use, x) return x - use(itemPrice) end)
    :Spring(50)
    :Map(function(use, x) return math.round(x) end)
    :Map(function(use, x) return `You have {x} coins` end)
@dphfox dphfox added enhancement New feature or request not ready - evaluating Currently gauging feedback labels Jan 24, 2025
@dphfox
Copy link
Owner Author

dphfox commented Jan 24, 2025

This would require some kind of utility to chain methods on UsedAs<T>. Presumably this would not construct a state object.

@dphfox
Copy link
Owner Author

dphfox commented Jan 24, 2025

In fact, instead of extending the metatable of all objects by default, this could be on-demand and support UsedAs<T> natively:

Pipe(usedAs)
    :ForValues(function(use, _ value)
        ...
    end)

Scope could be inferred from the originally piped object, under the stipulation that all chained operations do not require scopes for non-ScopeObject UsedAs<T>

@znotfireman
Copy link
Contributor

znotfireman commented Feb 18, 2025

Personally I lean towards the pipe API and not extending the metatable for state objects becauae it's forwards compatible with #301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request not ready - evaluating Currently gauging feedback
Projects
None yet
Development

No branches or pull requests

2 participants