tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

72 lignes · 5399 octetsBrut · Historique

ADR 0033: Opaque types, so a module can protect its invariants

Status: Proposed (2026-10-13, issue #139); needs engineering review (#44).

Context

  • pub type exports everything. A type's constructors and fields go with it, so a module can't protect an invariant. A smart constructor (email.parse, returning Result<Email, String>) is the usual way to model a validated value, but it's only a convention: any module can write Email("not validated").
  • A workaround that doesn't hold: a field of a private type (pub type Email { Email(value: String, token: Token) }) stops Email("x", email.Token) (E0104). But record update copies the token from a valid value: Email(..e, value: "forged") builds an unvalidated Email outside email.lse.
  • The target use is domain modelling (functional DDD over HTTP): values parsed once at the edge (Username, Email, Money) and trusted afterwards.

Decision

pub opaque type, as in Gleam. Inside its module, nothing changes. Outside, the type can be named and nothing else:

// email.lse
pub opaque type Email { Email(String) }

pub fn parse :: s: String -> Result<Email, String> {
  if string.contains s "@" { Ok(Email(s)) } else { Error("not an email: {s}") }
}

pub fn text :: e: Email -> String {
  match e {
    Email(s) -> s
  }
}

// main.lse
import email.{type Email}

fn greet :: e: Email -> String {
  "hello {email.text e}"
} // fine
fn forge :: -> Email {
  email.Email("x")
} // E0117

Outside the module, these are refused (E0117):

  • calling a constructor, qualified (email.Email(...)) or imported (import email.{Email}, refused at the import);
  • record update, Email(..e, ...): it is construction;
  • reading a field with .field;
  • a constructor pattern, in match, let or a function's parameters.

Still allowed outside: naming the type in signatures, annotations and import email.{type Email}; using the trait impls the module provides; passing, storing, comparing and sending values.

The diagnostic, E0117: "Email is opaque outside email", with what was attempted (building, matching, reading a field). It names the module, whose functions are the way in. opaque without pub is refused too (E0118): a type without pub is already private to its module.

The names. Until now, "opaque" meant a type without a body, whose values come from outside Louise (Int, FFI resources; grammar.md). Those are renamed abstract types, in the docs and comments only, since they have no keyword. So:

  • an abstract type has no constructors anywhere: its values come from the runtime or C;
  • an opaque type has constructors, visible only in its module.

opaque is a new keyword, meaningful only between pub and type.

Derives:

  • Reading derives are allowed: Eq, Ord, Hash, Show, Debug. The impl is generated in the type's module, where its constructors are visible, so the author chooses what callers can observe. (For a secret, such as a password, the author doesn't derive Show.)
  • Default is refused on an opaque type (E0408, as for Default on a type with several constructors). It would hand callers a value that skipped the module's checks. The author can write the impl by hand, through the smart constructor.
  • User-defined derives (ADR 0019) are allowed. Their derive function sees each field already mapped through the trait's method (Constructor<R>), so it can't build the type or read its raw fields. Future derives that build values from outside data (a MessagePack decoder, #140) must follow Default's rule or take the module's own parsing function (#140 proposes @derive(MsgPack(via: parse))).

What doesn't change: the interpreter, Perceus, the C backend and the runtime. An opaque type is a visibility rule, checked during name resolution and type checking.

Consequences

  • Domain values can be trusted: a function taking a Username knows it was validated, once, at the module's boundary.
  • The module provides every access. Callers need functions for each read (email.text), so an opaque type's module is a little longer than a plain one.
  • Tests of other modules can't fabricate values either: they go through the smart constructor, as production code does.
  • The language server offers no constructors or fields of an opaque type outside its module.

Alternatives considered

  • pub abstract type for this feature, keeping "opaque" for bodyless types. "Abstract" is ML's term, but "opaque" is what Gleam, the closest language in spirit, uses for exactly this. The owner chose opaque for the keyword and "abstract" for bodyless types.
  • Reusing E0104 ("private to its module"). One code less, but the type isn't private: it can be named, and only its representation is hidden. A separate code says that, and what to do instead.
  • Private construction, public reading (OCaml's private types): fields readable, construction refused. Not done: more rules for a case pub fn accessors cover. It can follow if opaque proves too coarse.
  • Per-constructor or per-field visibility. More expressive and more to learn. The same reasoning applies.
  • Default allowed as the author's explicit choice. Refused instead: a derived Default bypasses the smart constructor by construction, which is the invariant the keyword exists to protect.