liveBook · Manning

Opaque type

As explained in Functional Design and Architecture: Examples in Haskell

An opaque data type is a data type whose representation is hidden from client code behind an abstraction boundary.

An opaque type hides its representation behind a stable interface. Clients use the exposed operations without depending on private structure, so the implementation can change without breaking them. section 1.4.1

The pattern divides a subsystem into an unstable private implementation and a stable public interface. In C, a public opaque type and the procedures that operate on it form the interface; in typed domain models, hiding constructors can also prevent invalid states. section 1.4.1 section 3.3.2

Expose only the surface clients need:

  • Declare a type without revealing its structure, then provide functions for creating, using, and destroying its values. section 1.4.1
  • Use smart constructors when unrestricted constructors could combine values incorrectly; export the valid construction paths while hiding representation details. section 3.3.2
  • Choose deliberately between fully public types, internal modules for tests, and abstract data types with hidden structure. section 3.3.2

C interface with opaque pointers

A client can hold MYPROCOBJECT and MYPROCTYPE values and call visor_proc_CreateObject or visor_proc_DestroyObject, without accessing either structure's fields. The subsystem can therefore revise its representation behind the same procedural boundary. section 1.4.1

Preventing invalid device definitions

A public ComponentDef constructor permits an invalid controller built with a sensor passport. Smart constructors instead create sensor and controller definitions with the corresponding internal class, while the internal module keeps the representation available only at an explicit stability risk. section 3.3.2 section 3.3.2

                                                         #1
                                                         #2
typedef struct MYPROCOBJECT_Tag MYPROCOBJECT;            #3   
typedef struct MYPROCTYPE_Tag   MYPROCTYPE;
  
MYPROCOBJECT *visor_proc_CreateObject(MYPROCTYPE *type,  #4
                                      const char *name,
                                      void *context);
void          visor_proc_DestroyObject(MYPROCOBJECT *obj);

Inspect the compact C declaration that separates opaque type names from the procedures clients use. section 1.4.1

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Explains representation hiding through C declarations and typed constructors, then addresses testing access and internal stability trade-offs.
Examples
Uses C opaque pointers, device definitions, invalid component construction, smart constructors, and internal modules.
What you can do after
Apply via module interfaces and smart constructors that hide representation, preserve invariants, and permit controlled implementation changes.
How it connects
Connects opaque types with modularity, polymorphism, interfaces, abstract data types, and smart constructors.

Also explained in

Compare how 4 books explain opaque type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.