Reputation: 166
When I have a generic function in C#, where the generic argument defines the return type of the function only, F# seems incapable of infering the generic argument. I also do not know how to type-annotate this.
For example, the following C# function is very common in lightweight ORMs, such as dapper.net and ServiceStack-ORM-Lite:
public T Get<T>(string sql)
{
//....
var obj = Activator.CreateInstance<T>();
//....
return obj;
}
let say i try and use this in F#... firstly by type inference:
let GetCustomer sql : Customer =
MyCSharpObj.Get(sql)
Or, I try to annotate the type (not even sure if this syntax is correct):
let GetCustomer sql : Customer =
MyCSharpObj.Get<Customer>(sql)
In both instances, i get the compile error (on the MyCSharpObj.Get
line):
Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved.
How do a use a method of this kind in F# ?
Upvotes: 2
Views: 248
Reputation: 4860
From my comment:
let GetCustomer (sql : string) : Customer = ..
It is the "sql" it is having trouble identifying the type of, not the return value.
Upvotes: 4