0

I've recently started to really leverage Dialyzer in my Elixir work but I'm seeing a strange error that I'm not sure how to correct. I've got a behaviour defined as:

defmodule NeatCo.Engine.ConnectionBehaviour do

  @callback connect(binary, integer, keyword) :: term
  ...

end

I use the behaviour in another module as:

defmodule NeatCo.Engine.Connection do

  @behaviour NeatCo.Engine.ConnectionBehaviour

  def connect(host, port, opts) do
    ...
  end

end

Dialyzer (running in VS Code) yells at that with:

"The inferred type for the 1st argument is not a supertype of the expected type for the connect/3 callback in the NeatCo.Engine.Connection behaviour"

My (possibly mistaken) understanding is that String.t is an alias for binary, suggesting that they are the same thing and as such I'd expect this to be acceptable--I'm passing in a string.

I've tried a few things that haven't worked--changed it to String.t, binary() for example. I'm pretty sure I'm off in the weeds, picking flowers.

For the sake of clarity: I'm running elixir 1.8.2, and using the 0.7.0 version of the ElixirLS extension in VS Code. Because 1.8.2 isn't supported in recent versions of that extension, I had to downgrade it to the last version that supported 1.8.

jaydel
  • 14,389
  • 14
  • 62
  • 98
  • 1
    How is `host` being actually used in `NeatCo.Engine.Connection.connection/3`? It seems dialyzer is inferring something else. Also, have you tried running `mix dialyzer.clean` as suggested [here](https://github.com/ueberauth/guardian/issues/681#issuecomment-888504994=)? – sabiwara Jul 19 '22 at 13:27
  • excellent question. The implementation is this one line: `:gen_tcp.connect(host, port, opts)` That might be my issue...the first argument to that Erlang function does not appear to be a string at all. Also, for some reason, our installed dialyzer library doesn't provide `mix dialyzer.clean` as a task. – jaydel Jul 19 '22 at 13:52
  • It seems it accepts many types, but not what we call strings in Elixir. The type [`:inet.hostname()`](https://www.erlang.org/doc/man/inet.html#type-hostname) allows `string()`, which is [actually a charlist](https://hexdocs.pm/elixir/typespecs.html#the-string-type). For `dialyzer.clean`, you might need to to set the `MIX_ENV` if dialyxir is only installed for a specific environment, or you could probably manually delete the PLT files. – sabiwara Jul 19 '22 at 22:41

0 Answers0