F# 什么替换了不推荐的any\u to\u字符串函数?

F# 什么替换了不推荐的any\u to\u字符串函数?,f#,F#,我猜现在是Object.ToString(),但我不确定。。。谢谢 您可以使用sprintf: let a = [1;2;3] let b = sprintf "%A" a 您可以使用sprintf: let a = [1;2;3] let b = sprintf "%A" a 我认为获得任何_to_字符串功能的唯一方法是使用“%A”格式化程序。警告告诉你这一点 让任意字符串=sprintf“%A” 这不会调用.ToString()。对于列表之类的“简单”类型,.ToString()已经产生

我猜现在是Object.ToString(),但我不确定。。。谢谢

您可以使用sprintf:

let a = [1;2;3]
let b = sprintf "%A" a
您可以使用sprintf:

let a = [1;2;3]
let b = sprintf "%A" a

我认为获得任何_to_字符串功能的唯一方法是使用“%A”格式化程序。警告告诉你这一点

让任意字符串=sprintf“%A”

这不会调用.ToString()。对于列表之类的“简单”类型,.ToString()已经产生了一个很好的表示形式。但是当使用您自己的自定义类型时,%A格式化程序更有用。例如,在树结构的情况下,它沿着树行走

如果确实要对对象调用.ToString(),可以使用“%O”格式化程序

例如:

type Tree = Node of Tree * Tree | Leaf
let myTree = Node(Node(Leaf,Leaf),Node(Leaf,Node(Leaf,Leaf)))
在FSI中:

> myTree.ToString();;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%O" myTree;;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%A" myTree;;
val it : string = "Node (Node (Leaf,Leaf),Node (Leaf,Node (Leaf,Leaf)))"

我认为获得任何_to_字符串功能的唯一方法是使用“%A”格式化程序。警告告诉你这一点

让任意字符串=sprintf“%A”

这不会调用.ToString()。对于列表之类的“简单”类型,.ToString()已经产生了一个很好的表示形式。但是当使用您自己的自定义类型时,%A格式化程序更有用。例如,在树结构的情况下,它沿着树行走

如果确实要对对象调用.ToString(),可以使用“%O”格式化程序

例如:

type Tree = Node of Tree * Tree | Leaf
let myTree = Node(Node(Leaf,Leaf),Node(Leaf,Node(Leaf,Leaf)))
在FSI中:

> myTree.ToString();;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%O" myTree;;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%A" myTree;;
val it : string = "Node (Node (Leaf,Leaf),Node (Leaf,Node (Leaf,Leaf)))"

字符串
函数怎么样

> string [1..3];;
val it : string = "[1; 2; 3]"

字符串
函数怎么样

> string [1..3];;
val it : string = "[1; 2; 3]"

这是唯一可能的选择吗?我想是的。(更多的字符达到评论的最低限度)这是唯一其他可能的选择吗?我想是的。(更多字符以达到注释最小值)