prook
Hi,
I’m trying to get hang of specs, but I’ve hit a wall. I’m writing a thin facade over Nerves’ SystemRegistry, basically stuff like:
defmodule ConfigRegistry do
@spec update(SystemRegistry.scope(), any) :: {:ok, {map, map}} | {:error, term}
def update(scope, value) do
SystemRegistry.update([:config | scope], value)
end
end
Running mix dialyzer on that results in
lib/config_registry.ex:2:invalid_contract
Invalid type specification for function.
Function:
ConfigRegistry.update/2
Success typing:
@spec update([any()], _) :: %SystemRegistry.Transaction{
:delete_nodes => MapSet.t(_),
:deletes => MapSet.t(_),
:key => _,
:opts => Keyword.t(),
:pid => pid(),
:update_nodes => MapSet.t(_),
:updates => map()
}
A quick look at
https://github.com/nerves-project/system_registry/blob/master/lib/system_registry.ex#L54
tells me there are two updates, one that can be used standalone (this is the one I’m interested in), and the other one for use with an open transaction.
The functions are specced like this:
@spec update(one, scope, value :: any) :: Transaction.t() when one: Transaction.t()
@spec update(one, value :: any, opts :: Keyword.t()) ::
{:ok, {new :: map, old :: map}} | {:error, term}
when one: scope
I can sort of understand dialyxir’s complaint, if I make an assumtion that it cannot recognize [:config | scope] as something that is not a SystemRegistry.Transaction.t(). But that assumption seems very wrong.
So… Either (and most likely) I’m missing some crucial and basic knowledge of spec, there is an error in SystemRegistry.update spec, or a bug in Dialixyr.
How can I persuade Dialyxir my code will always call the standalone update function?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jeremyjh
A spec will not actually constrain the input to your
updatefunction. If someone calls your function with aTransaction.tfor the first parameter, you will invoke the other clause in Nerves. Did you try adding awhenclause to your update likewhen is_list(scope)?prook
Thanks for the input.
I won’t; Note that
[:config | scope]is passed toSystemRegistry.update.I did. No success.
peerreynders
This is where your mental model breaks down: there is only and there can only be one
update/3function. That function may be implemented in terms of multiple function clauses - but they all belong to the same function.The Erlang style of specs make that much clearer:
So strictly speaking the return type of
SystemRegistry.update/3is the sum typeTransaction.t() | {:ok, {new :: map, old :: map}} | {:error, term}.A more type-aware module API would have
SystemRegistry.update_with_scope/3SystemRegistry.update_with_transaction/3SystemRegistry.update/3that delegates appropriately.but given that Erlang/Elixir is dynamically typed you will run into functions that have only been typed after the fact rather than being designed with types in mind.
prook
…and I already slapped my forehead. Thank you.
peerreynders
Tried this approach?
or even more explicitly, intention revealing
prook
Pondering the topic some more…
There is one
SystemRegistry.update/3, but it actually does two very different things, or, better yet, it covers two completely different use cases; One is “do an update (and handle a transaction internally)”, the other is “add this update command to this transaction”.These actually should be two functions, and that’s where my thoughts derailed.
jeremyjh
That isn’t the success typing dialyzer is inferring, though.
peerreynders
… but I can see your point.
At least
tells us that dialyzer is fully aware that the first argument will be a list and not a
%Transaction{}- so there is no confusion there.jeremyjh
Yes my comment was so obviously wrong I didn’t bother to acknowledge it
@prook curious did you track this down?
prook
I’ve resolved Dialyxir’s complaints by replacing
with
This avoids ambiguous
SR.update/3withSR.commit/1, which is specced clearly, and matches my spec/requirements.Although I understand @peerreynders’ approach, I just cannot stomach the
if cannot_happen, then error "it did anyway!"pattern.As for why Dialyxir does not see – or whether it should see – that my
SR.update/3call will never returnSR.Transaction.t, I’m still as confused as when writing the OP.