示例代码:
Printf.ksprintf ignore "static string"
let dynamicString = Printf.StringFormat<unit>("dynamic string")
Printf.ksprintf ignore dynamicString // <- error
当我将静态字符串传递给 ksprintf 时,一切正常。但是如果我动态地形成一个字符串,则会出现编译错误:
类型'unit'与'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>
答案 0 :(得分:4)
格式字符串的正确类型不是unit
。它应该是
let dynamicString = Printf.StringFormat<unit,unit>("dynamic string")
要找到它,请创建一个这样的函数:
let test arg = Printf.ksprintf ignore arg
并查看arg