Why isn’t this allowed?
type Foo() =
static member Bar() = ()
let inline bar<^a>() = //ERROR: unexpected infix operator in pattern
(^a : (static member Bar : unit -> unit)())
//Hypothetical usage: let _ = bar<Foo>()
…but this works fine?
type Foo() =
static member Bar() = new Foo()
let inline bar() : ^a =
(^a : (static member Bar : unit -> ^a)())
let x : Foo = bar()
Are functions with statically resolved type parameters required to return an instance of the resolved type?
As you noticed, F# treats the sequence of characters
<^as an infix operator, so you need to separate them with a space. As to the question of when you need to explicitly specify constraints, I believe that the rule is that when you explicitly give a function type parameters then you also need to specify any necessary constraints. Otherwise, if F# can infer the type parameters and constraints, you don’t need to specify them.So this example from your question works fine:
as would this:
because there’s a generic type parameter, but you haven’t explicitly placed it on the function, and F# can infer the needed constraint.
On the other hand, if you try to modify your other example to omit the explicit generic parameter:
You’ll see that F# won’t allow this because there’s no way for it to figure out how to instantiate
^afor any given call ofbar(). Thus, you need to provide the type parameter explicitly, and once you do, you also have to explicitly provide the constraint.