Skip to content

Instantly share code, notes, and snippets.

@mrange
Created January 5, 2015 18:15
Show Gist options
  • Save mrange/9a3204e518131502d20c to your computer and use it in GitHub Desktop.
Save mrange/9a3204e518131502d20c to your computer and use it in GitHub Desktop.
F# Question 1
// I like to create class type that accepts a generic enhancer function
// I tried 3 variants and nothing gives me the result I need
// Anyone has any ideas?
// warning FS0064: This construct causes code to be less generic than indicated by
// the type annotations. The type variable 'U has been constrained to be type 'obj'.
type MyTest1<'T>(enhancer : 'U->'U) =
let whatever = 3
type MyTest2<'T>() =
// warning FS0064: This construct causes code to be less generic than indicated by
// the type annotations. The type variable 'U has been constrained to be type 'obj'.
let mutable enhancer : 'U->'U = fun x -> x
member x.Enhancer
with get () = enhancer
and set e = enhancer <- e
member x.Enhance (u : 'U) : 'U = enhancer u
type IEnhancer =
interface
abstract Enhance : 'U -> 'U
end
type MyTest3<'T>(enhancer : IEnhancer) =
member x.Enhance (u : 'U) : 'U = enhancer.Enhance u
let Create3<'T> (enhancer : 'U->'U) =
let e =
{
new IEnhancer with
// error FS0670: This code is not sufficiently generic. The type variable
// 'a could not be generalized because it would escape its scope.
member x.Enhance (u : 'U) : 'U = enhancer u
}
MyTest3<'T> (e)
[<EntryPoint>]
let main argv =
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment