What programming languages have something like Haskell’s `newtype` -


the haskell programming language has concept of newtypes: if write newtype foo = foo (bar), new type foo created isomorphic bar, i.e. there bijective conversions between two. properties of construct are:

  • the 2 types separate (i.e. compiler not allow use 1 other expected, without using explicit conversions).
  • they share same representation. in particular, conversion functions have run-time cost of 0 , return ”the same object” on heap.
  • conversion possible between such types , cannot mis-used, i.e. type safety preserved.

what other programming languages provide feature?

one example seems single-value-structs in c when used record accessors/constructors only. invalid candidates single-valued-structs in c when used casts, casts not checked compiler, or objects single member in java, these not share same representation.

related questions: does f# have 'newtype' of haskell? (no) , does d have 'newtype'? (not more).

frege has this, though, unlike in haskell there no keyword. instead, every product type 1 component newtype.

example:

data age = age int 

also, langugaes have nominal typing , allow define type in terms of should have feature. example oberon, modula-2 or ada. after

type age = integer;      {* kindly forgive syntax errors *} 

one couldn't confuse age , other quantity.


Comments