What does "requires" mean in F# function signature?

150 Views Asked by At

Please explain the following function signature which appeared when I hovered over the function in VS Code. I'm especially curious what exactly "requires" means and why 'b is 'a.

val handleSingleEvent:
    : Request
    -> 'b (requires :> seq<list<string>>)

Generic Parameters

  • 'b is 'a

Below is the code

let handleEvents (requests: Request list, reqEventQueue: EventQueue, session: Session)  =
      let rec handleSingleEvent (request: Request) : seq<list<string>> =
        seq {
          
              let eventObj = reqEventQueue.NextEvent()

              match eventObj.Type with
              | Event.EventType.REQUEST_STATUS -> yield processMiscEvents eventObj |> makeJson
              | Event.EventType.ADMIN -> yield processAdminEvent eventObj |> makeJson
              | Event.EventType.AUTHORIZATION_STATUS -> yield processAuthEvent eventObj session |> makeJson
              | Event.EventType.PARTIAL_RESPONSE ->
                yield processReferenceResponseEvent eventObj
                |> makeJson
                yield! handleSingleEvent request
              | Event.EventType.RESPONSE -> yield processReferenceResponseEvent eventObj |> makeJson
              | _ -> yield processMiscEvents eventObj |> makeJson
        } |> ignore
        handleSingleEvent request
      List.map (fun request -> handleSingleEvent request) requests

After adding the return type annotation seq<list<string>>, hovering over the function in VS Code now displays the function signature as

val handleSingleEvent:
    : Request
    -> seq<list<string>>

"requires" disappeared and "Generic Parameters `b is `a" disappeared.

1

There are 1 best solutions below

0
On BEST ANSWER

'requires' indicates a member constraint, meaning that the generic type argument is constrained to exhibit such member. As a brief example:

let inline f<'b  when 'b : (member Name : string)> (x: 'b) = x

The generic type 'b is now constrained to have a member Name that returns a string.

https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints

enter image description here