liveBook · Manning

Interface function

As explained in Elixir in Action, Third Edition

An interface function is a public function that exposes a component's operations while hiding its process creation and communication details.

An interface function is a public entry point that lets clients start or use a component without knowing how it creates processes or communicates through messages. In a server-process module, clients call functions such as start/0, while private implementation functions run the message loop in another process. section 5.3.1

The boundary separates client-facing operations from server behavior. Interface functions execute in the caller process and hide the communication protocol; implementation functions usually remain private and execute in the server process. section 5.3.1

A practical interface-function design typically provides:

  • A start function that spawns the long-running server process. section 5.3.1
  • Public operations that encode requests and delegate communication to the server. section 5.3.1
  • Helper functions that keep clients unaware of a generic server abstraction and its callbacks. section 6.1.3

Wrapping a generic server

KeyValueStore exposes start/0, put/3, and get/2. These functions call ServerProcess.start/1 and ServerProcess.call/2, while init/0 and handle_call/2 remain callback functions used internally by the generic process. section 6.1.3

Listing 6.5 Wrapping ServerProcess function calls (server_process.ex)
defmodule KeyValueStore do
 def start do
   ServerProcess.start(KeyValueStore)
 end

 def put(pid, key, value) do
   ServerProcess.call(pid, {:put, key, value})
 end

 def get(pid, key) do
   ServerProcess.call(pid, {:get, key})
 end

 ...
end

Shows the before-and-after boundary where application-specific functions wrap generic server-process calls. section 6.1.3

How Elixir in Action, Third Edition covers it

How deep it goes
Defines the client/server boundary and demonstrates wrappers, but stops short of broader API design guidance.
Examples
Uses DatabaseServer.start/0, run_async/2, and get_result/0, then KeyValueStore.start/0, put/3, and get/2.
What it assumes
Assumes familiarity with functions and processes while teaching how public calls relate to private loops and callbacks.
What you can do after
Build client-facing wrappers that operate concurrent components without exposing their process or communication implementation.
How it connects
Connects server processes, message passing, generic server processes, callback functions, and process creation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.