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

Does this replace do blocks and "explicit resource management"? #44

Open
theScottyJam opened this issue Mar 5, 2022 · 0 comments
Open

Comments

@theScottyJam
Copy link

How does this proposal relate to the do block and explicit resource management proposals? I'm noticing that the basic functionality of both of these proposals can trivially be implemented with this proposal. I know this proposal has had a bit of a rocky start (as stated in #35), but perhaps if we're able to resolve some of its core issues (with break/continue/etc) and simplify it, it may have a chance. And, considering that this single proposal is powerful enough to potentially replace two other proposals, it may be to our advantage to really give this proposal another chance.

The do-block proposal can be simulated by providing a built-in identity function that will simply call the expression-block passed into it.

// The do-block proposal
const result = do {
  let x = Math.random()
  x * x
}

// This proposal, with a built-in identity function called "run".
const result = run {
  let x = Math.random()
  x * x
}

Earlier versions of the resource-management proposal let you create a block using with, and allocate a resource for the duration of that block, like this:

with (const connection = openDbConnection()) {
  connection.createUser(username)
}

If we provide a built-in withResource() function that operates on an object that implemented a particular protocol, we could get similar results:

withResource (openDbConnection()) do (connection) {
  ...
}

They also had the ability to do resource-management logic with declarations, because it was a little more powerful this way (you could reuse the existing block), then later decided to drop the with syntax altogether to simplify things. While we can't simulate the declaration version of this proposal, we can still provide the with version, and that could be good enough to make it so we don't need the resource-management proposal at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant