Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type issue on decode!() #2

Closed
lucianoengel opened this issue Apr 2, 2020 · 2 comments
Closed

Type issue on decode!() #2

lucianoengel opened this issue Apr 2, 2020 · 2 comments

Comments

@lucianoengel
Copy link

lucianoengel commented Apr 2, 2020

Hello, I'm trying to shift from Jason to Jsonrs but I'm hitting some weird type issues. Dialyzer is going crazy overall. One example is the code below:

@spec deserialize(binary) :: {term, Metadata.t() | nil} | :error
 def deserialize(message) do
    case Jsonrs.decode!(message) do
      %{"route" => route, "reference" => reference, "payload" => payload} ->
        {payload, %Metadata{reference: reference, route: route}}
      %{"d" => payload} ->
        {payload, nil}
      _ ->
        :error
    end
 end

Dialyzer just give me this for the first case clause line

The pattern can never match the type.

Pattern:
%{
  <<114, 101, 102, 101, 114, 101, 110, 99, 101>> => _reference,
  <<114, 111, 117, 116, 101>> => _route,
  <<112, 97, 121, 108, 111, 97, 100>> => _payload
}

Type:
binary()

I don't understand how decode!() can be expecting a binary() type as return for the dialyzer run?

I did look at Jsonrs typing and everything seems correct, specs says decode!() should return term so I'm in a bit of a loss...

Important to notice is that I had no dialyzer issues at all with Jason and the only thing that changed in that function was the decode function module name

@benhaney
Copy link
Owner

benhaney commented Apr 2, 2020

Thanks for reporting this. Looks like the typespec for nif_decode!/1 was wrong, and dialyzer was appropriately narrowing the return type of decode!/2 because of that.
0.1.4 should fix this. Let me know.

@lucianoengel
Copy link
Author

@benhaney thanks for the quick reply! That did silence dialyzer around, closing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants