Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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