The Haskell programming language has a concept of newtypes
: If I write newtype Foo = Foo (Bar)
, then a new type Foo
is created that is isomorphic to Bar
, i.e. there are bijective conversions between the two. Properties of this construct are:
What other programming languages provide this feature?
One example seems to be single-value-structs in C when used with record accessors/constructors only. Invalid candidates would be single-valued-structs in C when used with casts, as the casts are not checked by the compiler, or objects with a single member in Java, as these would not share the same representation.
Related questions: Does F# have 'newtype' of Haskell? (No) and Does D have 'newtype'? (not any more).
Frege has this, though, unlike in Haskell there is no extra keyword. Instead, every product type with just one component is a newtype.
Example:
data Age = Age Int
Also, all langugaes that have nominal typing and allow to define a type in terms of another should have this feature. For example Oberon, Modula-2 or ADA. So after
type age = integer; {* kindly forgive syntax errors *}
one couldn't confuse an age and some other quantity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With