8.12

4.8 Private Implementation🔗ℹ

An internal name for a class can provide access to all internal methods of the class. For more selective control over sets of private methods, an interface can be privately implemented. Private implementation of a method also supports implementing a publicly known interface but without exposing the implementation of of the method to untrusted callers.

For example, suppose that we’d like to customize printing by implementing the Printable interface, but we don’t want a public print method. Printing and string conversion access customization methods using an internal name for Printable, so privately implementing Printable will achieve the goal.

To privately implement an interface, use private implements, and override the interfaces methods with private methods.

class Posn(x, y):

  private implements Printable

  private override describe(mode, recur):

    PrintDesc.list("⟨⟨⟨", [recur(x), recur(y)], "⟩⟩⟩")

> Posn(1, 2)

⟨⟨⟨1, 2⟩⟩⟩

> Posn(1, 2).describe(#'expr, Function.pass)

describe: no such field or method

  in value: ⟨⟨⟨1, 2⟩⟩⟩