为什么签名是...... - >结果<char*string>?

Ale*_*don 2 .net f#

我正在关注斯科特的讲话,他带来的一个例子是:

type Result<'a> = 
    | Success of 'a
    | Failure of string
let pchar (charToMatch, input) =
    if System.String.IsNullOrEmpty (input) then
        Failure "no input!"
    else
        let first =  input.[0]
        if  first = charToMatch then
            let remaining = input.[1..]
            Success (charToMatch, remaining)
        else
            let msg = sprintf "exepecting '%c' got '%c'" charToMatch first
            Failure msg
Run Code Online (Sandbox Code Playgroud)

Visual Studio代码显示该pchar函数的签名是:

char*string->Result<char*string> :

vscode截图

退货类型应该只是Result<'a>

kag*_*oki 5

Result<'a>是一种泛型类型,具有'a类型参数.当您编写Success (charToMatch, remaining)编译器时,将此泛型类型参数推断为char和的元组string:Result<char*string>.你可以写Success (),你会得到一个Result<unit>类型.

同样在F#中,当您在括号中用您的逗号列出函数的参数时(charToMatch, input),这意味着您的函数期望一个元组,这意味着您不能使用currying.为了使curry可用,你应该定义这样的函数: let pchar charToMatch input = ...