I am trying to create a generic function that requires an argument of its type that it is a record type and that it has a specific property. Here is a sample that generates the corresponding compiler error:
let foo<'a> (a : 'a) =
a' = { a with bar = "baz" }
a'
Having compiled this, I get an error message The record label bar is not defined.
I tried adding the following type restriction:
let foo<'a when 'a : (member Id : string)> =
but this also did not compile, complaining that This code is not sufficiently generic. The type variable ^a when ^a : (member get_Int : ^a -> string) could not be generalized because it would escape its scope.
Is there a way to specify a type constraint that will allow me to do this correctly?
source
share