Skip to content

Commit

Permalink
Multi-clause with blocks work
Browse files Browse the repository at this point in the history
  • Loading branch information
ashton314 committed Aug 13, 2024
1 parent b5b29d7 commit 54b78ef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ end

You can bind the result of some expression to a variable/pattern at an actor with `with`. In the case of a higher-order choreography (seen above) this is whatever was on node `OtherActor` when `other_chor` executed. You may also use `with` for binding local expressions, as seen in the `exchange_message` example under § Function syntax.

Right now you can only have one `<-` in the expression and there can only be one actor that binds variables.


## Creating a choreography

To create a choreography, start by making a module, and writing the choreography with the `defchor` macro.
Expand Down Expand Up @@ -359,6 +356,10 @@ If you find any bugs or would like to suggest a feature, please [open an issue o

We will collect change descriptions here until we come up with a more stable format when changes get bigger.

- v0.4.3; 2024-08-13

Multi-clause `with` blocks work.

- v0.4.2; 2024-08-07

Bugfix: projecting local expressions that call out to an Erlang module.
Expand Down
9 changes: 9 additions & 0 deletions lib/chorex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,15 @@ defmodule Chorex do
end
end

def project(
{:with, meta, [{:<-, _, _} = hd | [{:<-, _, _} | _] = rst]},
env,
label,
ctx
) do
project({:with, meta, [hd, [do: {:with, meta, rst}]]}, env, label, ctx)
end

# Local expressions of the form Actor.thing or Actor.(thing)
def project({{:., _, [{:__aliases__, _, _} | _]}, _, _} = expr, env, label, ctx) do
project_local_expr(expr, env, label, ctx)
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Chorex.MixProject do
def project do
[
app: :chorex,
version: "0.4.2",
version: "0.4.3",
elixir: "~> 1.16",
start_permanent: Mix.env() == :prod,
description: description(),
Expand Down
7 changes: 4 additions & 3 deletions test/generalized_functions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ defmodule GeneralizedFunctionsTest do
defmodule MyCrypto do
defchor [AliceC, BobC] do
def run(AliceC.(msg)) do
with BobC.({pub, priv}) <- BobC.gen_key() do
with BobC.({pub, priv}) <- BobC.gen_key(),
AliceC.(whatever) <- AliceC.(40 + 2) do
BobC.(pub) ~> AliceC.(key)
exchange_message(AliceC.encrypt(msg <> "\n love, Alice", key), BobC.(priv))
exchange_message(AliceC.encrypt(msg <> "\n love, Alice (" <> to_string(whatever) <> ")" , key), BobC.(priv))
end
end

Expand Down Expand Up @@ -46,6 +47,6 @@ defmodule GeneralizedFunctionsTest do
["hello, world"])

assert_receive {:chorex_return, AliceC, :letter_sent}
assert_receive {:chorex_return, BobC, "hello, world\n love, Alice"}
assert_receive {:chorex_return, BobC, "hello, world\n love, Alice (42)"}
end
end

0 comments on commit 54b78ef

Please sign in to comment.