Last active
August 29, 2015 14:14
-
-
Save eiriktsarpalis/087775afe656ad31c311 to your computer and use it in GitHub Desktop.
NewType in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// current type abbreviations in F# | |
type A = Guid | |
type B = Guid | |
let foo (x : A) = x.ToString() | |
let b = B.NewGuid() | |
foo(b) // type checks | |
// proposed newtype abbreviations | |
newtype A = Guid | |
newtype B = Guid | |
let foo (x : A) = x.ToString() | |
let b = B.NewGuid() | |
foo(b) // type error | |
// newtype abbreviations should generate explicit conversion methods | |
let g : Guid = B.ToGuid b | |
let b : B = B.FromGuid g | |
// alternative attribute-based approach | |
[<NewType>] | |
type A = Guid | |
[<NewType>] | |
type B = Guid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In other words, the struct approach is probably the better way of solving this.