Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule TypeClass do
- # (...)
- defmacro definst(class, opts, do: body) do
- [for: datatype] = opts
- quote do
- # The type class protocol's instance implementation goes into
- # `type_class.Proto.datatype` (in the case of `Monoid`, this will
- # be `Witchcraft.Monoid.Proto.Function`), as `defimpl/2` below
- # defines its own context.
- instance = Module.concat([unquote(class), Proto, unquote(datatype)])
- defimpl unquote(class).Proto, for: unquote(datatype) do
- import TypeClass.Property.Generator.Custom
- Module.register_attribute(__MODULE__, :force_type_instance, [])
- @force_type_instance false
- @doc false
- def __custom_generator__, do: false
- defoverridable __custom_generator__: 0
- unquote(body)
- @doc false
- def __force_type_instance__, do: @force_type_instance
- end
- cond do
- unquote(class).__force_type_class__() ->
- IO.warn("...")
- instance.__force_type_instance__() ->
- IO.warn("...")
- true ->
- unquote(datatype) |> conforms(to: unquote(class))
- end # /
- end # /
- end # /
- # /
- # (...) _____/
- # /
- defmacro conforms(datatype, opts) do
- class = Keyword.get(opts, :to)
- quote do
- # (Dependency check here, omitted for brevity.)
- for {prop_name, _one} <- unquote(class).Property.__info__(:functions) do
- TypeClass.Property.run!(unquote(datatype), unquote(class), prop_name)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement