Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created June 8, 2011 15:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/1014623 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/1014623 to your computer and use it in GitHub Desktop.
Generic Restrictions
let inline name (x : ^T) = (^T : (member Name : string) (x))
let inline age (x : ^T) = (^T : (member Age : int) (x))
let inline nameAndAge x = (name x, age x)
type Person(name : string, age : int) =
member __.Name = name
member __.Age = age
type Employee(name : string, age : int, employeeId : string) =
member __.Name = name
member __.Age = age
member __.EmployeeId = employeeId
let person = Person("Matt", 5)
let employee = Employee("Matt", 5, "12345")
name person // prints Matt
age person // prints 5
nameAndAge person // prints ("Matt", 5)
name employee // prints Matt
age employee // prints 5
nameAndAge employee // prints ("Matt", 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment