Custom types

Contents

Custom types#

type Name underlying creates a new named type. Two named types with the same underlying type are still distinct; an explicit conversion bridges them.

Brand two opaque IDs.

type UserID string
type OrgID  string

var u UserID = "u-1"
var o OrgID  = "o-1"

Convert one to the other explicitly.

_ = UserID(o)                // explicit conversion required

Named types are how the operator brands primitives so the compiler catches mixed-up identifiers.

References#

  • Primitives for the underlying types brands wrap.

  • Conversion for the explicit-conversion rule.

  • OOP for attaching methods to a named type.