OvermindDL1 / protocol_ex

Elixir Extended Protocol
https://hex.pm/packages/protocol_ex
48 stars 5 forks source link

warns of unused alias when using module for pattern #64

Closed DaTrader closed 2 years ago

DaTrader commented 2 years ago

The code below compiles and works, but gives: warning: unused alias MyStruct which can be avoided by removing the alias and specifying the full module name in defimpl_ex, but it'd be nice if there wasn't a warning in the first place.

defmodule MyApp.Stuff.MyStruct do
  def new( args) do
    IO.inspect( args, label: "args")
  end
end

import ProtocolEx

defprotocol_ex JsonDecodable, as: module do
  def decode( module, json)
end

defmodule JsonDecoder do
  def decode( target, json, decodes_elem) do
    args =
      for elem <- json,
          decoded = decodes_elem.( elem),
          reduce: %{}
        do
        map ->
          { k, v} = decoded
          Map.put( map, k, v)
      end

    { :ok, target.new( args)}
  end
end

alias MyApp.Stuff.MyStruct

defimpl_ex MyStruct, MyStruct, for: JsonDecodable do
  def decode( module, %{} = json) do
    JsonDecoder.decode( module, json, &decode_elem/1)
  end

  def decode_elem( { "a", v}) do
    { :a, v}
  end

  def decode_elem( _), do: nil
end
DaTrader commented 2 years ago

Btw, another issue all together, but see the decode/2 function in the my defimpl_ex? It needs to be copy-pasted for each such defimpl_ex although being the same. I tried defining it as a __using__ macro in the JsonDecoder module and using it from the defimpl_ex but it does not work that way. Your defimpl_ex insists it's defined directly in it. Can this too be accommodated?

Thanks

DaTrader commented 2 years ago

I see the issue was already solved, but the README.md is not updated to installing the latest release (still stuck at 0.3.0).