liveBook · Manning

Interface

As explained in Julia as a Second Language

An interface is a defined set of operations and behavioral expectations through which software components interact independently of their implementations.

An interface is a set of operations and behavioral expectations that lets components interact without knowing each other’s implementation details. Like a keyboard and mouse used with different computers, a shared interface lets the same client code work with different implementations. section 12.1

One function, different arrays

In Julia, arrays and ranges are both subtypes of AbstractArray. A function that adds their elements can accept either one without needing separate methods. The same function rejects a tuple, however, because Tuple is not in the AbstractArray hierarchy—even though Julia’s sum function accepts tuples. section 12.1

Listing 12.1 Function for adding up elements in an abstract array
function addup(xs::AbstractArray)
    total = 0           #1
    for x in xs
        total += x
    end
    total               #2
end

This function uses iteration to add elements from any accepted AbstractArray. section 12.1

Julia interfaces are informal: there is no special interface-definition syntax. Methods belong to functions rather than types, so documentation tells implementers which functions and behaviors belong to an interface. An abstract type can identify a family of implementations, but defining a subtype alone does not supply its required methods. section 12.2

Documenting a tank interface

A Tank interface can describe totalmass, drymass, and propellant operations. A shared refill! function uses those operations to fill a tank. A new Tank subtype still needs appropriate methods: merely giving it fields with matching names does not define totalmass or drymass. Declaring and documenting the functions makes their intended roles easier to discover. section 12.2

An interface can also provide useful default behavior. For a Cipher, string encryption and decryption can map character-level operations over a string. Concrete ciphers implement those character operations; fallback methods can report an explicit error when an implementation is missing. A password service can then use a Cipher without depending on a particular cipher implementation. section 15.6

Listing 15.10 Defining an abstract cipher interface
abstract type Cipher end

function encrypt(cipher::Cipher, char::Char)
   error("Implement encrypt(::", typeof(cipher), ", char)")
end

function decrypt(cipher::Cipher, char::Char)
   error("Implement decrypt(::", typeof(cipher), ", char)")
end

function encrypt(cipher::Cipher, message::AbstractString)
   map(ch -> encrypt(cipher, ch), message)
end
function decrypt(cipher::Cipher, ciphertext::AbstractString)
   map(ch -> decrypt(cipher, ch), ciphertext)
end

The Cipher interface combines explicit errors for missing character methods with reusable string methods. section 15.6

Implementing an existing interface can make established functions available to a custom type. For example, making a staged rocket iterable allows functions such as map, filter, sum, and collect to work with it. section 12.7

How Julia as a Second Language covers it

How deep it goes
Explains informal interface definition through documentation, required methods, fallback errors, and reusable default methods.
Examples
Uses arrays and ranges, a propellant tank, an iterable rocket, and interchangeable ciphers in a password service.
What it assumes
Assumes familiarity with Julia types and methods while explaining how interface functions are documented.
What you can do after
Build interfaces from scratch using documented functions, subtype methods, and optional default implementations.
How it connects
Connects interfaces with abstract types, subtyping, iteration, generic functions, and object-oriented programming.

Also explained in

Compare how 9 books explain interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.