defprotocol bug?

55 views
Skip to first unread message

Maximiliano Guzenski

unread,
Aug 1, 2016, 2:54:29 PM8/1/16
to elixir-lang-core
Hi, 
I'm trying a simple to implement defprotocol/defimpl but I recevei always a error:
ps.: Activity is a Ector module, I using phoenix

defprotocol D4.Policy do
  def scoped(a, query, conn)
end

defimpl D4.Policy, for: D4.Activity do
  def scoped(a, query, conn) do
    IO.puts "hi"
  end
end

D4.Policy.scoped(D4.Activity, 1, 2)

error -->

** (Protocol.UndefinedError) protocol D4.Policy not implemented for D4.Activity

    (d4) web/lib/policy.ex:1: D4.Policy.impl_for!/1

    (d4) web/lib/policy.ex:3: D4.Policy.scoped/3


Ben Wilson

unread,
Aug 1, 2016, 3:01:59 PM8/1/16
to elixir-lang-core
As shown in the documentation on defprotocol, you need to define a struct in the D4.Activity module, and then pass a %D4.Activity{} struct to the protocol function. As it is you're just passing in a module name which is an Atom, and there is no protocol implementation for atoms.

Maximiliano Guzenski

unread,
Aug 1, 2016, 3:36:33 PM8/1/16
to elixir-lang-core
Yes, my mistake... struct(D4.Activity) resolved it... thanks !
Reply all
Reply to author
Forward
0 new messages