如何printfn int*int*int类型

dem*_*mas 1 f#

> (1, 2, 3);;
val it : int * int * int = (1, 2, 3)

> printfn "%d" (1, 2 ,3);;

  printfn "%d" (1, 2 ,3);;
  --------------^^^^^^^

stdin(2,15): error FS0001: The type '('a * 'b * 'c)' is not compatible with any of the types byte,int16,int32,int64,sbyt
e,uint16,uint32,uint64,nativeint,unativeint, arising from the use of a printf-style format string
Run Code Online (Sandbox Code Playgroud)

如何在不创建附加功能的情况下打印此类型?

Rob*_*ert 6

您可以使用格式化程序%A打印任何类型:

printfn "%A" (1, 2, 3);;
Run Code Online (Sandbox Code Playgroud)

否则,您将需要解压缩您的元组以打印它,因为没有特定于元组的格式化程序.

  • 只是添加,如果你在一些大的递归数据结构上尝试它,"%A"会很慢..因为它使用反射来导航数据结构并打印结果 (2认同)