I don't have access to an F# compiler right now, so I may be wrong, but I think the problem may go away if you make the function arguments tupled instead of curried. E.g. instead of

GetCustomAttribute<'T> (typ:Type) inherited

try

GetCustomAttribute<'T>(typ:Type, inherited)

I'm speculating that since the curried version creates a function that returns another function, somehow that's the source of the 'T escape' issue...

By on 12/18/2008 3:07 PM ()

Thanks for the reply. I'm getting the same error messages using the functions below
member x.GetCustomAttribute<'T> ((typ:Type), inherited) =
let attributes = typ.GetCustomAttributes(typeof<'T>, inherited)
match attributes.Length with
| v when v > 0 -> Some(attributes.[0] :?> 'T)
| _ -> None

and
member x.EncodeObject (data:obj) =
let noLazyLoad = x.GetCustomAttribute<DetailsAttribute> (data.GetType(),false)
let noLazyLoad2 = x.GetCustomAttribute<System.FlagsAttribute> (data.GetType(), false)
()

The error messages are
1::This code is less generic than required by its annotations because the explicit type variable 'T' could not be generalized. It was constrained to be 'DetailsAttribute'.
2::This expression has type FlagsAttribute but is here used with type DetailsAttribute.
3::A use of the function 'Json.GetCustomAttribute.3' does not match a type inferred elsewhere. The inferred type of the function is Json -> Type * bool -> DetailsAttribute option. The type of the function required at this point of use is Json -> Type * bool -> DetailsAttribute option This error may be due to limitations associated with generic recursion, i.e. type inference requires that generic functions and members are instantiated in only one way within their own recursive scope, e.g. within a 'let rec' collection or within a group of classes. Consider splitting these recursive bindings into two groups

By on 12/18/2008 4:36 PM ()

(Still guessing)

The other thing to try is to add all the type annotations:

member x.GetCustomAttribute<'T> (typ:Type, inherited:bool) : option<'T> =

By on 12/19/2008 7:05 AM ()

That compiled.

Is there any reason why type parameters are not allowed with let bindings as
member x.DoIT<'T> a b :'T compiles but
let DoIT<'T> a b : 'T fails with the message
Explicit type parameters may only be used on module or member bindings

Thank you.

By on 12/19/2008 3:59 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper