16

我觉得不得不问这个问题真是个菜鸟,但这让我很难过。

我设置了这样的格式字符串:

let fs = "This is my format test %s"

然后我尝试像这样使用它:

let s = sprintf fs "testing"

当我这样做时,我收到此错误:

//stdin(26,17): error FS0001: The type 'string' is not compatible with the type 'Printf.StringFormat<('a -> 'b)>'

所以我然后尝试了这个:

let s = sprintf (Printf.StringFormat fs) "test"

REPL 对此作出回应:

//stdin(28,18): error FS1124: Multiple types exist called 'StringFormat', taking different numbers of generic parameters. Provide a type instantiation to disambiguate the type resolution, e.g. 'StringFormat<_>'.

所以我然后尝试了这个:

let s = sprintf (Printf.StringFormat<string> fs) "test" 

我明白了:

//stdin(29,18): error FS0001: The type ''a -> 'b' does not match the type 'string'

我错过了一些非常明显的东西吗?这是在 Mac 上从 Xamarin Studio F# 交互窗口使用 F# 3.0。

4

1 回答 1

15

所以你实际上需要创建StringFormat一个函数类型如下

> sprintf (Printf.StringFormat<string->string>("Hello %s")) "World";;
  val it : string = "Hello World"

在规范的第6.3.16节中,显示了一个示例。

于 2013-10-05T21:51:16.530 回答