Skip to content

one_way should return self() pid, not :ok #16

@ericridgeway

Description

@ericridgeway

In your blog post leading up to some of these ideas, you use a cool trick.

When the original logic code decided it needed to be wrapped in a server, you made sure the server setter's returned pid, so you were still returning the new state as usual in functional language

(although now the state was "opaquely" represented by a pid, instead of the naked %{} or whatever other internal data structure)

As you mentioned, this kept the original api the exact same. It was beautiful! If you wrote tests or had other modules already using the "just logic" code, you didn't have to change anything. You could even keep using pipelines since the "state" was passed back at each step

  def new() do
    { :ok, names } = GenServer.start_link(Kv.Server, %{})
    names
  end
  
  def lookup(names, name) do
    GenServer.call(names, {:lookup, name})
  end
  
  def store(names, name, value) do
    GenServer.cast(names, { :store, name, value })
    names
  end

Anything that used to do:

kv =
  Kv.new()
  |> Kv.store("Cat", "meow")

assert Kv.lookup(kv, "Cat") == "meow

Still works. Pipelines don't break. The internal api changing from pure logic to being wrapped in a genserver didn't show to outsiders

Could this library's "one_way" maintain that idea by any chance and return the pid instead of :ok

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions