Skip to content

Instantly share code, notes, and snippets.

@lukaszkrzywizna
Last active April 20, 2023 20:44
Show Gist options
  • Save lukaszkrzywizna/b3dc325e9579d474e8193c71fcbcb770 to your computer and use it in GitHub Desktop.
Save lukaszkrzywizna/b3dc325e9579d474e8193c71fcbcb770 to your computer and use it in GitHub Desktop.
SRTP F# .Net 6
type MyType =
| MyCase
static member statFun x = match x with MyCase -> "I'm static member function!"
static member statFunUnit () = "I'm static member function with unit param!"
static member statFunGeneric (x, y) =
match x with MyCase -> $"I'm static member function with generic param with value = %O{y}!"
static member statProp = "I'm static member property!"
member this.objProp = match this with MyCase -> "I'm object member property!"
member this.objFunGen x = match this with MyCase -> $"I'm object member function with param with value = %O{x}!"
member this.objFunUnit () = match this with MyCase -> "I'm object member function with unit param!"
let inline statFun< ^a when ^a: (static member statFun: ^a -> string)> (a: ^a) =
(^a: (static member statFun: ^a -> string) a)
let inline statFunUnit< ^a when ^a: (static member statFunUnit: unit -> string)> () =
(^a: (static member statFunUnit: unit -> string) ()) // NOTE: unit param needed
let inline statFunGeneric< ^a, ^b when ^a: (static member statFunGeneric: ^a * ^b -> ^b)> (a: ^a) (b: ^b) =
(^a: (static member statFunGeneric: ^a * ^b -> ^b) (a,b))
let inline statProp< ^a when ^a: (static member statProp: string)> () =
(^a: (static member statProp: string) ())
let inline objProp< ^a when ^a: (member objProp: string)> (a: ^a) =
(^a: (member objProp: string) a)
let inline objFunGen< ^a, ^b when ^a: (member objFunGen: ^b -> string)> (a: ^a) (b: ^b) =
(^a: (member objFunGen: ^b -> string) (a, b))
let inline objFunUnit< ^a when ^a: (member objFunUnit: unit -> string)> (a: ^a) =
(^a: (member objFunUnit: unit -> string) a) // NOTE: no unit param needed
let test () =
let my = MyCase
[ statFun my
statFunUnit<MyType>()
statFunGeneric my "test"
statProp<MyType> ()
objProp my
objFunGen my 5
objFunUnit my ] |> List.iter(printfn "%s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment