Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. defmodule TypeClass do
  2. # (...)
  3.  
  4. defmacro definst(class, opts, do: body) do
  5. [for: datatype] = opts
  6.  
  7. quote do
  8.  
  9. # The type class protocol's instance implementation goes into
  10. # `type_class.Proto.datatype` (in the case of `Monoid`, this will
  11. # be `Witchcraft.Monoid.Proto.Function`), as `defimpl/2` below
  12. # defines its own context.
  13. instance = Module.concat([unquote(class), Proto, unquote(datatype)])
  14.  
  15. defimpl unquote(class).Proto, for: unquote(datatype) do
  16. import TypeClass.Property.Generator.Custom
  17.  
  18. Module.register_attribute(__MODULE__, :force_type_instance, [])
  19. @force_type_instance false
  20.  
  21. @doc false
  22. def __custom_generator__, do: false
  23. defoverridable __custom_generator__: 0
  24.  
  25. unquote(body)
  26.  
  27. @doc false
  28. def __force_type_instance__, do: @force_type_instance
  29. end
  30.  
  31. cond do
  32. unquote(class).__force_type_class__() ->
  33. IO.warn("...")
  34.  
  35. instance.__force_type_instance__() ->
  36. IO.warn("...")
  37.  
  38. true ->
  39. unquote(datatype) |> conforms(to: unquote(class))
  40. end # /
  41. end # /
  42. end # /
  43. # /
  44. # (...) _____/
  45. # /
  46. defmacro conforms(datatype, opts) do
  47. class = Keyword.get(opts, :to)
  48.  
  49. quote do
  50. # (Dependency check here, omitted for brevity.)
  51.  
  52. for {prop_name, _one} <- unquote(class).Property.__info__(:functions) do
  53. TypeClass.Property.run!(unquote(datatype), unquote(class), prop_name)
  54. end
  55. end
  56. end
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement