Add a with
form
#1
Labels
No Label
accepted
bug
clj
documentation
enhancement
errors
infrastructure
later
next
now
optimization
proposal
question
research
semantics
syntax
ux
vm
wontfix
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: twc/ludus#1
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Based on Elixir's
with
: better than a separatebind
for working with possibly-failing or complexly conditional chained results.A
with
clause is a sort of backwards function clause:{pattern} <- {expression}
. The expression is evaluated first, and then matched against the pattern.Each
with
clause is evaluated, in order. The expression is evaluated first, and then the result is tested against the pattern. If the pattern matches, names are bound and used in the evaluation of the next clause's expression, which is then tested against a pattern, and so on. If everything works right, the expression is then evaluated.If, however, at any given point, the result of the expression does not match against its pattern, the result is then sent to the
else
block, which contains bog-standardmatch
clauses. The failing value is then tested against these clauses normally.I don't quite know when we'd use this in Ludus, but having just written a parser and interpreter in Clojure, I am here to tell you this is the solution to no early return.
For reference: https://hexdocs.pm/elixir/1.16/docs-tests-and-with.html#with
Note: the Elixir
with
form does not require anelse
block, but rather simply returns the value from the first non-matching clause in thewith
block.Syntactically, this should be a bit different than what we have here. All the with bindings are on the left-hand side, and then there's a single expression to execute. Elixir actually does
with (<clause>, )+ do: <expr>
.There's no reason to use the backwards arrow here; the
=
operator between left and right models perfectly well the control flow.So instead, you might instead try:
What that does is allow fewer tokens, and a kind of consistency.
if
/then
/else
vs.with
/then
/else
. Moreover, that obviatesif let
: we only needwith
here. And it should be easy enough to either have a single binding without braces, or multiple bindings with them.The thing here is that this is a lovely model for a thing, but feels like it does not yet have much use in Ludus, since there isn't (yet) much that returns a result tuple. Wait to add this until it feels necessary for what we are doing in Ludus.