错误-在elixir中实现协议
我正在运行以下代码:错误-在elixir中实现协议,elixir,Elixir,我正在运行以下代码: ExHubic.Request.request({:get, "/account", nil, :hubic}, ExHubic) 并获取以下错误: ** (Protocol.UndefinedError) protocol ExHubic.Request not implemented for {:get, "/account", nil, :hubic} (ex_hubic) lib/request.ex:1: ExHubic.Request.impl_
ExHubic.Request.request({:get, "/account", nil, :hubic}, ExHubic)
并获取以下错误:
** (Protocol.UndefinedError) protocol ExHubic.Request not implemented for {:get, "/account", nil, :hubic}
(ex_hubic) lib/request.ex:1: ExHubic.Request.impl_for!/1
(ex_hubic) lib/request.ex:6: ExHubic.Request.request/2
此错误消息向我表明,未为类型ExHubic.Query.Hubic.t
实现协议,该类型定义为@type t:{atom,String.t,any,:Hubic}
我创建的类型很可能有问题,但我看不出来
一些背景:
defprotocol ExHubic.Request do
@moduledoc false
@spec request(query :: ExHubic.Query.t, client :: atom)
:: ExHubic.Query.http_query_t
def request(query, client)
end
defimpl ExHubic.Request, for: ExHubic.Query.Hubic do
@spec request(query :: ExHubic.Query.Hubic.t, client :: atom)
:: {:ok, ExHubic.Client.response_t} | {:error, ExHubic.Client.response_t}
def request({method, uri, params, :hubic} = query, client) do
implementation_details
end
end
defmodule ExHubic.Query do
@moduledoc false
@type t :: {atom, String.t, any, atom}
end
defmodule ExHubic.Query.Hubic do
@type t :: {atom, String.t, any, :hubic}
@spec account() :: __MODULE__.t
def account(), do: {:get, "/account", :nil, :hubic}
end
这里有两个问题,首先,您使用atom(模块名)作为协议。您需要使用内置类型或结构,如中所述。这意味着要向模块中添加defstruct:
defmodule ExHubic.Query.Hubic do
defstruct [:method, :uri, :params, service: :hubic]
@type t :: %__MODULE__{method: atom, uri: String.t, params: any, service: :hubic}
def account(), do: {:get, "/account", :nil, :hubic}
end
defprotocol ExHubic.Request do
def request(query, client)
end
defimpl ExHubic.Request, for: ExHubic.Query.Hubic do
@spec request(Query.t, atom) :: any
def request(query, client) do
IO.inspect query
end
end
然后,您可以将其用于:
ExHubic.Request.request(%ExHubic.Query.Hubic{method: :get, params: nil, service: :hubic, uri: "/account"}, client)
这里有两个问题,首先,您使用atom(模块名)作为协议。您需要使用内置类型或结构,如中所述。这意味着要向模块中添加defstruct:
defmodule ExHubic.Query.Hubic do
defstruct [:method, :uri, :params, service: :hubic]
@type t :: %__MODULE__{method: atom, uri: String.t, params: any, service: :hubic}
def account(), do: {:get, "/account", :nil, :hubic}
end
defprotocol ExHubic.Request do
def request(query, client)
end
defimpl ExHubic.Request, for: ExHubic.Query.Hubic do
@spec request(Query.t, atom) :: any
def request(query, client) do
IO.inspect query
end
end
然后,您可以将其用于:
ExHubic.Request.request(%ExHubic.Query.Hubic{method: :get, params: nil, service: :hubic, uri: "/account"}, client)
我上次的尝试遇到了问题。你的答案很可能是正确的,但我还没有成功地实现这一点。我几周内都不会坐在电脑桌前。关于元组不被允许的那一点绝对是正确的,它让我走上了正确的道路。但我还不确定是查询还是客户机应该是第一个参数。当我回到办公桌前时,我需要做更多的调查。谢谢。将元组更改为结构修复了它。出于参考目的:Files:,我在以前的尝试中遇到了问题。你的答案很可能是正确的,但我还没有成功地实现这一点。我几周内都不会坐在电脑桌前。关于元组不被允许的那一点绝对是正确的,它让我走上了正确的道路。但我还不确定是查询还是客户机应该是第一个参数。当我回到办公桌前时,我需要做更多的调查。谢谢。将元组更改为结构修复了它。仅供参考:文件:,