Newtype

Noun · Development

Definitions

  1. A pattern (and keyword in Haskell) for creating a distinct type that wraps an existing type with zero runtime overhead, providing type safety by preventing accidental mixing of semantically different values — e.g., `struct Meters(f64)` vs `struct Seconds(f64)` in Rust, even though both wrap `f64`.

    In plain English: Creating a brand-new type that's really just an existing type underneath, so the compiler can catch mistakes like mixing up user IDs and order IDs even though both are numbers.

    Example: "The newtype wrapper around `u64` means the compiler catches it if you accidentally pass a `UserId` where a `PostId` is expected."

Related Terms