提问人:LearnerX 提问时间:4/15/2022 最后编辑:LearnerX 更新时间:4/20/2022 访问量:119
F# 函数签名中的“requires”是什么意思?
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
添加返回类型注解后,将鼠标悬停在 VS Code 中的函数上现在将函数签名显示为seq<list<string>>
val handleSingleEvent:
: Request
-> seq<list<string>>
“requires” 消失了,“Generic Parameters 'b is 'a' 消失了。
答:
1赞
citykid
4/19/2022
#1
“requires”表示成员约束,这意味着泛型类型参数被约束为显示此类成员。举个简单的例子:
let inline f<'b when 'b : (member Name : string)> (x: 'b) = x
泛型类型 'b 现在被限制为具有返回字符串的成员 Name。
https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/generics/constraints
评论