prook

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?

Showing Posts 1 to 10

jeremyjh

jeremyjh

A spec will not actually constrain the input to your update function. If someone calls your function with a Transaction.t for the first parameter, you will invoke the other clause in Nerves. Did you try adding a when clause to your update like when is_list(scope) ?

prook

prook OP

Thanks for the input.

I won’t; Note that [:config | scope] is passed to SystemRegistry.update.

I did. No success.

peerreynders

peerreynders

This is where your mental model breaks down: there is only and there can only be one update/3 function. 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:

-spec foo(pos_integer()) -> pos_integer()
         ; (integer()) -> integer().

So strictly speaking the return type of SystemRegistry.update/3 is the sum type Transaction.t() | {:ok, {new :: map, old :: map}} | {:error, term}.

A more type-aware module API would have

  • SystemRegistry.update_with_scope/3
  • SystemRegistry.update_with_transaction/3
  • And a convenience function SystemRegistry.update/3 that 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

prook OP

…and I already slapped my forehead. Thank you. :slight_smile:

peerreynders

peerreynders

Tried this approach?

  def update(scope, value) do
    case SystemRegistry.update([:config | scope], value) do
      %SystemRegistry.Transaction{} ->
        {:error, :undefined} # i.e. this will never happen
      result ->
        result
    end
  end

or even more explicitly, intention revealing

  @spec update(SystemRegistry.scope(), any) :: {:ok, {map, map}} | {:error, term}
  def update(scope, value),
    do: update_with_scope(scope, value)

  @spec update_with_scope(SystemRegistry.scope(), any) :: {:ok, {map, map}} | {:error, term}
  defp update_with_scope(scope, value) do
    case SystemRegistry.update([:config | scope], value) do
      %SystemRegistry.Transaction{} ->
        {:error, :undefined} # i.e. this will never happen
      result ->
        result
    end
  end
prook

prook OP

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

jeremyjh

That isn’t the success typing dialyzer is inferring, though.

peerreynders

peerreynders

  • I wasn’t sure whether the dialyzer error was completely quoted.
  • I wondered whether dialyzer was just complaining about the missing case.

… but I can see your point.

At least

@spec update([any()], _) :: %SystemRegistry.Transaction{

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

jeremyjh

Yes my comment was so obviously wrong I didn’t bother to acknowledge it :slight_smile:

@prook curious did you track this down?

prook

prook OP

I’ve resolved Dialyxir’s complaints by replacing

SystemRegistry.update([@mount | scope], value)

with

    SystemRegistry.transaction()
    |> SystemRegistry.update([@mount | scope], value)
    |> SystemRegistry.commit()

This avoids ambiguous SR.update/3 with SR.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/3 call will never return SR.Transaction.t, I’m still as confused as when writing the OP.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews