Conditional "use" #2824
Replies: 2 comments 7 replies
-
Use doesn't have any particular semantics, it's just sugar for a function call. Given the function could do anything how would we know what the no-op could be? What would your example look like rewritten using the regular function call syntax instead of use? |
Beta Was this translation helpful? Give feedback.
-
I realized that it can be achieved this way without the syntactic sugar for a non-block expression: use <- iterator.yield("foo")
use <- case add_bar {
True -> iterator.yield("bar", _)
False -> noop
}
use <- iterator.yield("baz")
iterator.empty() given the function fn noop(cont: fn() -> a) -> a {
cont()
} One drawback I can think of is that the mandatory Another is that doing two yields in the branch requires something like: use <- iterator.yield("foo")
use <- case add_bar {
True -> fn(cont) {
use <- iterator.yield("bar 1")
iterator.yield("bar 2", cont)
}
False -> noop
}
use <- iterator.yield("baz")
iterator.empty() while with the proposed syntactic sugar it would look more consistent: use <- iterator.yield("foo")
case add_bar {
True -> {
use <- iterator.yield("bar 1")
use <- iterator.yield("bar 2")
}
False -> use <- noop()
}
use <- iterator.yield("baz")
iterator.empty() |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The proposal
It might be nice to have syntactic sugar for a conditional
use <- something
, for exampleuse <- iterator.yield(...)
.This video demonstrates the desugaring for a proposed syntax in which the final
use <- foo(args)
within each branch of the case expression becomesfoo(args, next)
wherenext
stands for the code following the case expression as a function.use-within-case.mp4
noop
is just a function such thatnoop(next)
becomesnext()
in order not to do yield anything within that branch.Alternatives
As I realized during the following discussion, the same can be achieved in the current language as follows:
Or in the case of a single
iterator.yield
in a branch, not needing a block:It is not too bad without the additional syntactic sugar, just a bit asymmetric with the explicit "next" parameter.
noop
Whether the syntactic sugar is added or not, the
noop
function might be useful for anyone utilizing a conditionaluse
. Should it perhaps be in the standard library?Original message
EDIT: Updated the message to hopefully be clearer, as the original message resulted in confusion in the following discussion.
The original message
Is there a nice way to do a conditional
use <- something
, for exampleuse <- iterator.yield(...)
? I'm new to the language and I didn't get suggestions when I asked on the Discord server.In case there isn't, my proposal follows.
I would like to be able to write code such as
where the
use()
stands in for whatever syntax would be adopted for a no-op "use".My proposal is the syntactic sugar where
would desugar into
Given a result value,
would desugar into
Beta Was this translation helpful? Give feedback.
All reactions