使用动态字符串时ksprintf类型错误

ksprintf type error when using dynamic string

示例代码:

Printf.ksprintf ignore "static string"

let dynamicString = Printf.StringFormat<unit>("dynamic string")
Printf.ksprintf ignore dynamicString // <- error

当我将静态字符串传递给 ksprintf 时,一切正常。但是,如果我动态形成一个字符串,则会出现编译错误:

The type 'unit' does not match the type 'string'

P.S。在 FSharp.Core:

的来源中找到
/// <summary>Represents a statically-analyzed format when formatting 
/// arguments of the format operation and the last the overall return type.</summary>
type StringFormat<'T,'Result> = Format<'T, unit, string, 'Result>

/// <summary>Represents a statically-analyzed format when formatting builds a string. 
/// The type parameter indicates the arguments and return type of the 
/// format operation.</summary>
type StringFormat<'T> = StringFormat<'T,string>

格式字符串的正确类型不是 unit。应该是

let dynamicString = Printf.StringFormat<unit,unit>("dynamic string")  

要找出答案,请创建如下函数:

let test arg = Printf.ksprintf ignore arg

并查看 arg

的生成类型