我们有一个函数,将像Int32.TryParse这样的东西从使用byref转换为使用返回值的选项。
let inline ToOptionFunc refFunction x =
match refFunction x with
| true, value -> Some value
| false, _ -> None这样的东西停止了在.NET内核中编译新的重载到TryParse:
let Int32TryParse (x:string) =
ToOptionFunc Int32.TryParse x // A unique overload for method 'TryParse' could not be determined (...)我尝试了很多事情,却意外地把它写成这样:
let Int32TryParse (x:string) =
x |> ToOptionFunc Int32.TryParse我只是不明白为什么这会编译,而前者则不会。
发布于 2019-03-13 21:13:45
在.NET核心中,Int32.TryParse函数有一些额外的重载--它可以解析string或ReadOnlySpan<char>。原始代码停止工作,因为编译器不知道要使用哪种重载。
ToOptionFunc Int32.TryParse x.它从左到右,当它到达Int32.TryParse时,它还不知道x (它有一个将其限制为string的类型注释),而且如果不知道x,它就无法知道您需要哪个TryParse。x |> ToOptionFunc Int32.TryParse.从左到右,它知道x是string,因此它推断传递给ToOptionFunc的函数也必须使用string --当它检查Int32.TryParse时,它已经可以唯一地确定重载。总结是,管道有很好的性能指导类型检查!
https://stackoverflow.com/questions/55150600
复制相似问题