Skip to content

Commit

Permalink
Merge pull request #2 from sodapopcan/allow-defeffect-with-no-parens
Browse files Browse the repository at this point in the history
Do not require parens for `defeffect`
  • Loading branch information
smoes authored Jun 29, 2024
2 parents e885a86 + d4f7ec6 commit 8e91e04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/efx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ defmodule Efx do

defmacro defeffect(fun, do_block) do
{name, ctx, args} = fun
args = ensure_list(args)
module = __CALLER__.module

# we do this to not get warnings for wildcard params in functions
Expand Down Expand Up @@ -216,10 +217,16 @@ defmodule Efx do
end

@spec spec_name({any(), any(), list()}) :: name :: atom()
defp spec_name({_, _, a}), do: List.first(a) |> elem(0)
defp spec_name({_, _, a}) do
a = ensure_list(a)
List.first(a) |> elem(0)
end

@spec spec_arity(tuple()) :: arity()
defp spec_arity({:"::", _, [{_, _, args} | _]}), do: Enum.count(args)
defp spec_arity({:"::", _, [{_, _, args} | _]}) do
args = ensure_list(args)
Enum.count(args)
end

@spec already_exists?(module(), atom(), arity()) :: boolean()
def already_exists?(module, name, arity) do
Expand All @@ -230,4 +237,8 @@ defmodule Efx do
end
)
end

@spec ensure_list(list() | nil) :: list()
defp ensure_list(nil), do: []
defp ensure_list(list), do: list
end
6 changes: 6 additions & 0 deletions test/efx_case_global_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ defmodule EfxCaseGlobalTest do
assert [] = TestAgent.get()
end
end

describe "style" do
test "zero-artity may be defined without parens" do
assert EfxExample.without_parens() == :ok
end
end
end
5 changes: 5 additions & 0 deletions test/support/efx_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ defmodule EfxCase.EfxExample do
defeffect multi_fun(string) do
string
end

@spec without_parens :: atom
defeffect without_parens do

Check warning on line 33 in test/support/efx_example.ex

View workflow job for this annotation

GitHub Actions / Test on OTP 25.0.4 / Elixir 1.14.0

got "@impl EfxCase.EfxExample" for function without_parens/0 but this behaviour does not specify such callback. There are no known callbacks, please specify the proper @behaviour and make sure it defines callbacks
:ok
end
end

0 comments on commit 8e91e04

Please sign in to comment.