为什么我会收到sprintf与类型字符串不兼容的错误?

时间:2015-09-20 23:35:25

标签: f#

我有这个F#代码无法编译。

let wiql = 
    "SELECT [State], [Title], [Work Item Type], [ReferenceId]
     FROM WorkItems
     WHERE [System.TeamProject] = '%s'
     AND [Work Item Type] IN ('Product Backlog Item', 'Bug')
     AND ([ReferenceId] CONTAINS 'IM' OR [ReferenceId] CONTAINS 'CM' OR [ReferenceId] CONTAINS 'RF')"

let wiql = sprintf wiql projectName

第二个wiql下的红色波浪形的错误是: 类型'string'与'Printf.StringFormat<('a - >'b)>'

类型不兼容

我尝试用查询字符串中的projectName替换%s。

感谢您提供有关如何解决此问题的任何提示,

马修

更新:我尝试了一个问题的答案,我的问题是重复的,但我无法让它工作。该示例使用printfn,但是当我使用sprintf尝试相同的操作时,它会失败。我们之间没有看到它们之间存在一些差异。

这是我的F#Interactive会话,用于演示:

> let wiql = 
        Printf.TextWriterFormat<string -> unit>(
            "SELECT [State], [Title], [Work Item Type], [ReferenceId]
             FROM WorkItems
             WHERE [System.TeamProject] = '%s'
             AND [Work Item Type] IN ('Product Backlog Item', 'Bug')
             AND ([ReferenceId] CONTAINS 'IM' OR [ReferenceId] CONTAINS 'CM' OR [ReferenceId] CONTAINS 'RF')");;

val wiql : PrintfFormat<(string -> unit),System.IO.TextWriter,unit,unit>

> printfn wiql "DRILQUIP";;
SELECT [State], [Title], [Work Item Type], [ReferenceId]
             FROM WorkItems
             WHERE [System.TeamProject] = 'DRILQUIP'
             AND [Work Item Type] IN ('Product Backlog Item', 'Bug')
             AND ([ReferenceId] CONTAINS 'IM' OR [ReferenceId] CONTAINS 'CM' OR [ReferenceId] CONTAINS 'RF')
val it : unit = ()
> let x = sprintf wiql "DRILQUIP";;

  let x = sprintf wiql "DRILQUIP";;
  ----------------^^^^

stdin(19,17): error FS0001: The type 'unit' does not match the type 'System.IO.TextWriter'

我也试过Printf.TextWriterFormat字符串&gt;但那也失败了。

更新: 发现它!

let wiql:Printf.StringFormat<string -> string> = "SELECT [State] FROM WorkItems WHERE [System.TeamProject] = '%s'"

let x = sprintf wiql "DRILQUIP"

printfn "%s" x

使用Printf.StringFormat而不是Printf.TextWriterFormat来准备与sprintf一起使用的格式。如果我理解类型签名字符串&gt;它是一种具有1个可替换字符串占位符并返回字符串的格式。

这也有效。

Printf.StringFormat<_>

0 个答案:

没有答案
相关问题