gpt4 book ai didi

.net - F# 字符串连接运算符的用途是什么 ^

转载 作者:行者123 更新时间:2023-12-03 23:44:57 26 4
gpt4 key购买 nike

我正在阅读 docs并点击这一段

There are two ways to concatenate strings: by using the + operator or by using the ^ operator. The + operator maintains compatibility with the .NET Framework string handling features.



我把它打进了我的 linqpad,它运行正常,但我确实收到了警告

This construct is for ML compatibility. Consider using the '+' operator instead



在网上搜索了更多细节,但想出了 nix。

任何人都可以详细说明 ^ 的预期功能吗?

最佳答案

简答 - 使用 +而不是 ^就像警告所暗示的那样,运算符是可以互换的。

更长的答案:查看 FSharp.Core,您会发现这两个操作符都是内联的并遵循 ​​System.String.Concat .

here^ :

let inline (^)     (x:string) (y:string) = System.String.Concat(x,y)

here+ :
let inline (+) (x: ^T) (y: ^U) : ^V = 
AdditionDynamic<(^T),(^U),(^V)> x y
...
when ^T : string and ^U : string =
(# "" (System.String.Concat((# "" x : string #),(# "" y : string #))) : ^T #)

+定义更复杂,但归结为同一件事。
^似乎是 F# 中从 OCaml 采用的众多(实际上已经过时)结构之一。在将 OCaml 代码轻松移植到 F# 被认为是该语言的一个重要卖点的时候:
value prefix ^ : string -> string -> string
s1 ^ s2 returns a fresh string containing the concatenation of the strings s1 and s2.

我真的不记得曾经看过使用 ^ 的代码在实践中,如果你问我,我无法告诉 F# 有这个运算符。

关于.net - F# 字符串连接运算符的用途是什么 ^,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56506478/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com