Skip to content

Commit

Permalink
Merge pull request #83 from jechol/dev/fix-use-witchcraft
Browse files Browse the repository at this point in the history
Fix `__using__` for `override_kernel` and `except`
  • Loading branch information
jechol authored Nov 15, 2021
2 parents 70317fa + dfd4453 commit 1c634c3
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 47 deletions.
4 changes: 3 additions & 1 deletion lib/witchcraft/applicative.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ defclass Witchcraft.Applicative do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Apply, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/apply.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ defclass Witchcraft.Apply do
@type fun :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Functor, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/arrow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ defclass Witchcraft.Arrow do
@type t :: fun()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Category, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/bifunctor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ defclass Witchcraft.Bifunctor do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Functor, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/category.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ defclass Witchcraft.Category do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Semigroupoid, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ defclass Witchcraft.Chain do
@type link :: (any() -> Chain.t())

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Apply, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/comonad.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ defclass Witchcraft.Comonad do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Extend, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/extend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ defclass Witchcraft.Extend do
@type colink :: (Extend.t() -> any())

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Functor, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
17 changes: 10 additions & 7 deletions lib/witchcraft/foldable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,28 @@ defclass Witchcraft.Foldable do
@type t :: any()

defmacro __using__(opts \\ []) do
{:ok, new_opts} =
Keyword.get_and_update(opts, :except, fn except ->
{:ok, [length: 1, max: 2, min: 2] ++ (except || [])}
end)
overrides = [length: 1, max: 2, min: 2]
excepts = Keyword.get(opts, :except, [])

if Access.get(opts, :override_kernel, true) do
kernel_imports = Macro.escape(except: overrides -- excepts)
module_imports = Macro.escape(except: excepts)

quote do
use Witchcraft.Semigroup, unquote(opts)
use Witchcraft.Ord, unquote(opts)

import Kernel, unquote(new_opts)
import unquote(__MODULE__), unquote(opts)
import Kernel, unquote(kernel_imports)
import unquote(__MODULE__), unquote(module_imports)
end
else
module_imports = Macro.escape(except: Enum.uniq(overrides ++ excepts))

quote do
use Witchcraft.Semigroup, unquote(opts)
use Witchcraft.Ord, unquote(opts)

import unquote(__MODULE__), unquote(new_opts)
import unquote(__MODULE__), unquote(module_imports)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/functor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ defclass Witchcraft.Functor do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/monad.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ defclass Witchcraft.Monad do
use Witchcraft.Chain

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Applicative, unquote(opts)
use Witchcraft.Chain, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/monoid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ defclass Witchcraft.Monoid do
@type t :: any()

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Semigroup, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down
17 changes: 10 additions & 7 deletions lib/witchcraft/ord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ defclass Witchcraft.Ord do
import Kernel, except: [<: 2, >: 2, <=: 2, >=: 2]

defmacro __using__(opts \\ []) do
{:ok, new_opts} =
Keyword.get_and_update(opts, :except, fn except ->
{:ok, [<: 2, >: 2, <=: 2, >=: 2] ++ (except || [])}
end)
overrides = [<: 2, >: 2, <=: 2, >=: 2]
excepts = Keyword.get(opts, :except, [])

if Access.get(opts, :override_kernel, true) do
kernel_imports = Macro.escape(except: overrides -- excepts)
module_imports = Macro.escape(except: excepts)

quote do
import Kernel, unquote(new_opts)
import Kernel, unquote(kernel_imports)
use Witchcraft.Semigroupoid, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
else
module_imports = Macro.escape(except: Enum.uniq(overrides ++ excepts))

quote do
use Witchcraft.Semigroupoid, unquote(opts)
import unquote(__MODULE__), unquote(new_opts)
import unquote(__MODULE__), unquote(module_imports)
end
end
end
Expand Down
19 changes: 12 additions & 7 deletions lib/witchcraft/semigroup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ defclass Witchcraft.Semigroup do
@type t :: any()

defmacro __using__(opts \\ []) do
{:ok, new_opts} =
Keyword.get_and_update(opts, :except, fn except ->
{:ok, [<>: 2] ++ (except || [])}
end)
overrides = [<>: 2]
excepts = Keyword.get(opts, :except, [])

if Access.get(opts, :override_kernel, true) do
kernel_imports = Macro.escape(except: overrides -- excepts)
module_imports = Macro.escape(except: excepts)

quote do
import Kernel, unquote(new_opts)
import unquote(__MODULE__), unquote(opts)
import Kernel, unquote(kernel_imports)
import unquote(__MODULE__), unquote(module_imports)
end
else
quote do: import(unquote(__MODULE__), unquote(new_opts))
module_imports = Macro.escape(except: Enum.uniq(overrides ++ excepts))

quote do
import unquote(__MODULE__), unquote(module_imports)
end
end
end

Expand Down
19 changes: 12 additions & 7 deletions lib/witchcraft/semigroupoid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ defclass Witchcraft.Semigroupoid do
@type t :: any()

defmacro __using__(opts \\ []) do
{:ok, new_opts} =
Keyword.get_and_update(opts, :except, fn except ->
{:ok, [apply: 2] ++ (except || [])}
end)
overrides = [apply: 2]
excepts = Keyword.get(opts, :except, [])

if Access.get(opts, :override_kernel, true) do
kernel_imports = Macro.escape(except: overrides -- excepts)
module_imports = Macro.escape(except: excepts)

quote do
import Kernel, unquote(new_opts)
import unquote(__MODULE__), unquote(opts)
import Kernel, unquote(kernel_imports)
import unquote(__MODULE__), unquote(module_imports)
end
else
quote do: import(unquote(__MODULE__), unquote(new_opts))
module_imports = Macro.escape(except: Enum.uniq(overrides ++ excepts))

quote do
import unquote(__MODULE__), unquote(module_imports)
end
end
end

Expand Down
19 changes: 12 additions & 7 deletions lib/witchcraft/setoid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ defclass Witchcraft.Setoid do
import Kernel, except: [==: 2, !=: 2]

defmacro __using__(opts \\ []) do
{:ok, new_opts} =
Keyword.get_and_update(opts, :except, fn except ->
{:ok, [==: 2, !=: 2] ++ (except || [])}
end)
overrides = [==: 2, !=: 2]
excepts = Keyword.get(opts, :except, [])

if Access.get(opts, :override_kernel, true) do
kernel_imports = Macro.escape(except: overrides -- excepts)
module_imports = Macro.escape(except: excepts)

quote do
import Kernel, unquote(new_opts)
import unquote(__MODULE__), unquote(opts)
import Kernel, unquote(kernel_imports)
import unquote(__MODULE__), unquote(module_imports)
end
else
quote do: import(unquote(__MODULE__), unquote(new_opts))
module_imports = Macro.escape(except: Enum.uniq(overrides ++ excepts))

quote do
import unquote(__MODULE__), unquote(module_imports)
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/witchcraft/traversable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ defclass Witchcraft.Traversable do
@type link :: (any() -> Traversable.t())

defmacro __using__(opts \\ []) do
module_imports = [except: Keyword.get(opts, :except, [])]

quote do
use Witchcraft.Foldable, unquote(opts)
use Witchcraft.Functor, unquote(opts)
import unquote(__MODULE__), unquote(opts)
import unquote(__MODULE__), unquote(module_imports)
end
end

Expand Down

0 comments on commit 1c634c3

Please sign in to comment.