Unit.Quantity protocol (Hyper v0.1.0)

Copy Markdown View Source

A one-dimensional physical quantity backed by a single canonical integer (bytes, nanoseconds, bytes/sec, ...). Implementing this is all a unit type needs to get +, -, and the ordering operators from Unit.Operators.

Implementations go through the type's own public constructor/accessor, so the opaque struct is read and rebuilt without ever being poked from the outside. Quantities are signed: subtraction may legitimately yield a negative value (a deficit), and nothing clamps it.

Summary

Types

t()

All the types that implement this protocol.

Functions

The canonical scalar (e.g. bytes) backing the quantity.

A quantity of the same dimension as quantity, carrying scalar n.

Types

t()

@type t() :: term()

All the types that implement this protocol.

Functions

value(quantity)

@spec value(t()) :: integer()

The canonical scalar (e.g. bytes) backing the quantity.

with_value(quantity, n)

@spec with_value(t(), integer()) :: t()

A quantity of the same dimension as quantity, carrying scalar n.

Unit.Operators calls this to rebuild a result through the type's own public constructor, so the opaque struct is never poked from the outside. quantity only selects the implementation (the protocol dispatches on it); its scalar is unused.