-
Notifications
You must be signed in to change notification settings - Fork 2
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
Scope composition #96
Comments
It dawn's on me that we already support this when blocks have no arguments (this is how else-if chains work) so the extension is pretty feasible, even without the >> operator. |
If file.With returned a monad error eg optional<file.File>, you could maybe have a second scope that is unpack_or("...") which would not enter the scope (on that note, file could just do that, right?) The second scope should be able to forward with >> to a third nested scope |
Could you provide a sample of what this would look like? I'm having trouble understanding. |
where you define |
This is (afaict) the |
I see. Yes, you could do this, having the monad scope try to unwrap and enter the body if it succeeds and pass over if it fails. However, the file.With scope could do the same. In fact it already does if you don't provide an error handling scope. The question is, what do you do if you really do want a scope for error handling? |
Let me try again to understand the error issue. file.With (filename) open [f: file.File] {
file.Lines (f) each [line: []char] {
ProcessLine(line)
}
} onError {
// do something
} to something like this file.With (filename) open >> file.Lines () each [line: []char] {
ProcessLine(line)
} // where does onError go? This is the close curly brace of file.Lines Let's remove some sugar, though. file.With (filename) open >> {
file.Lines () each [line: []char] {
ProcessLine(line)
}
} onError { ... } That seems ok. We can even compress it back to 3 lines if we want file.With (filename) open >> { file.Lines () each [line: []char] {
ProcessLine(line)
} } onError { ... } How do I feel about this? Effective Icarus now warns you about onError scope placement when using syntax sugar, but other than that ... fine? |
All this said, I'm generally in favor of the |
There seems to be a somewhat common pattern with nesting scopes, for which I think the following example is illustrative:
We've specified
f
but only use it to pass to the next scope. It would be nice if we could compose these scopes directly.This proposal is for a non-overloadable operator (tentatively spelled
>>
) which can connect nested scopes, passing the arguments provided from the outer block directly as arguments to the inner scope. Rewritten, the above code would be:There are a few caveats here:
Lines
andeach
to resolve parsing ambiguities. It also would allow us to feed further arguments ifLines
had accepted any. So more carefully, the arguments provided from theopen
block are passed as a prefix of the arguments toLines
, and more can be placed in the parentheses.file.With
error scope anymore. We could place it beforeopen
, meaning this mechanism only works for the last block in the scope. This solution feel satisfying to me though.It's worth mentioning what my Advent Of Code day 2 solution would look like. If we adopted this, and changed the parsing rules slightly to be smarter about newlines (I think this is possible, but I'm not 100% positive), the solution would look like:
Which I think is particularly elegant.
The text was updated successfully, but these errors were encountered: