"requires" 在 F# 函数签名中是什么意思?
What does "requires" mean in F# function signature?
请解释当我将鼠标悬停在 VS Code 中的函数上时出现的以下函数签名。我特别好奇“需要”到底是什么意思,为什么 'b 是 'a。
val handleSingleEvent:
: Request
-> 'b (requires :> seq<list<string>>)
通用参数
- 'b 是 'a
下面是代码
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
添加 return 类型注释 seq<list<string>>
后,将鼠标悬停在 VS Code 中的函数上现在将函数签名显示为
val handleSingleEvent:
: Request
-> seq<list<string>>
“requires”消失了,“Generic Parameters `b is `a”消失了。
'requires' 指示成员约束,这意味着泛型类型参数被约束以显示该成员。举个简单的例子:
let inline f<'b when 'b : (member Name : string)> (x: 'b) = x
泛型类型 'b 现在被限制为具有 returns 字符串的成员名称。
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints
请解释当我将鼠标悬停在 VS Code 中的函数上时出现的以下函数签名。我特别好奇“需要”到底是什么意思,为什么 'b 是 'a。
val handleSingleEvent:
: Request
-> 'b (requires :> seq<list<string>>)
通用参数
- 'b 是 'a
下面是代码
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
添加 return 类型注释 seq<list<string>>
后,将鼠标悬停在 VS Code 中的函数上现在将函数签名显示为
val handleSingleEvent:
: Request
-> seq<list<string>>
“requires”消失了,“Generic Parameters `b is `a”消失了。
'requires' 指示成员约束,这意味着泛型类型参数被约束以显示该成员。举个简单的例子:
let inline f<'b when 'b : (member Name : string)> (x: 'b) = x
泛型类型 'b 现在被限制为具有 returns 字符串的成员名称。
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints