What does a caret mean in a Type Annotation?

I sometimes see type annotations like this:

'a -> ^b
'a option -> ^d

I am slightly confused by the ^ symbol in this kind of type annotation, what is that?

The ^ is used instead of the ' for Statically Resolved Type Parameters or SRTP for short. Note that in F# 7, a lot of effort was made to make SRTP a lot more ergonomic for use, and part of that was to make the ' work at least most of the time for SRTP as well as for “normal” generics. So nowadays, ^ can often be replaced with '. As a short answer, you can probably just think of your examples as 'a -> 'b and 'a option -> 'd. A quick example of SRTP in action might look like

let inline getName (thingWithName : ^a when ^a : (member Name : string)) =
  thingWithName.Name

type Person = { Name : string; Age : int }
type Pet = { Name : string; Owner : Person }

let scott = { Name = "Scott"; Age = 30 }
let fido = { Name = "Fido"; Owner = scott }

printfn "%s" (getName scott)
printfn "%s" (getName fido)

Note that I can pass either a Person or a Pet into getName even though they’re different types and don’t share an interface because of SRTP.

1 Like

In future you might find the F# Symbol and operator reference useful for syntax that’s hard to search for.

1 Like