Skip to content

Instantly share code, notes, and snippets.

@pirrmann
Created April 9, 2015 09:05
Show Gist options
  • Save pirrmann/757b1ff276244d4e7f5b to your computer and use it in GitHub Desktop.
Save pirrmann/757b1ff276244d4e7f5b to your computer and use it in GitHub Desktop.
Statically resolved member constraints
type SyntaxToken = string
type X =
{
SomeProperty: string
} with
member this.AddModifiers(tokens:SyntaxToken array) =
{ this with
SomeProperty = sprintf "%s + %s" (this.SomeProperty) (System.String.Join(",", tokens)) }
type Y =
| SomeDUType of int
| SomeOtherDUType of int with
member this.AddModifiers(tokens:SyntaxToken array) =
match this with
| SomeDUType i -> SomeOtherDUType (i+1)
| SomeOtherDUType i -> SomeDUType (i+1)
let inline addModifiers< ^T when ^T : (member AddModifiers : SyntaxToken array -> ^T)> tokens (input:^T) =
(^T : (member AddModifiers : SyntaxToken array -> ^T) (input, tokens))
// tests
let x = { SomeProperty = "Test" } |> addModifiers [|"a";"b"|]
let y = SomeDUType 1 |> addModifiers [|"a";"b"|]
@vbfox
Copy link

vbfox commented Apr 9, 2015

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment