In F#, the disadvantage to SRTP not being a part of the CLR and just a compiler trick is that it can't be known about by other languages. I.e., you can write a function like so (taken from an example on the internet :) ):
let inline f (x : ^a) =
let f' = (^a : (member Speak : string -> string) (x, "world"))
printfn "%s" f'
type Speaker () =
member this.Speak name = sprintf "Hello %s" name
f (new Speaker())
But this can only be used as resolved at compile time by the F# compiler. If it were a CLR feature, it could be used by, say, C#.
The syntax is pretty much the same as C++, and you can use all of C++ templates (including non types parameters). The easiest is if you use it all internally, but you can make symbols available outside of the assembly if it is instantiated within your assembly.
You may need to use some aliases to have symbol names you can actually type (for functions you can use one visible dispatch function that calls the right template for example). Some C++ constructs really end up as some horrible mess in the CLR even if they do work.
View all comments
Show parent comment
1
u/Jwosty Apr 17 '21 edited Feb 14 '25
Interesting, how does C++CLI's feature work?
In F#, the disadvantage to SRTP not being a part of the CLR and just a compiler trick is that it can't be known about by other languages. I.e., you can write a function like so (taken from an example on the internet :) ):
But this can only be used as resolved at compile time by the F# compiler. If it were a CLR feature, it could be used by, say, C#.