F# 包装printf并仍然获取参数
但是 我知道这与Printf.textWritePerformat与简单字符串有关,但即使像在cprintf c fmt:Printf.textWritePerformat中那样为fmt指定类型,我也无法让kprintf部分工作。我读了很多关于日志记录的答案,但我还是不明白。可以使用格式参数的cprintf吗?首先,你的cprintf函数非常好,你可以使用不同数量的参数。问题仅在于cprintfn功能:F# 包装printf并仍然获取参数,f#,F#,但是 我知道这与Printf.textWritePerformat与简单字符串有关,但即使像在cprintf c fmt:Printf.textWritePerformat中那样为fmt指定类型,我也无法让kprintf部分工作。我读了很多关于日志记录的答案,但我还是不明白。可以使用格式参数的cprintf吗?首先,你的cprintf函数非常好,你可以使用不同数量的参数。问题仅在于cprintfn功能: cprintfn ConsoleColor.Yellow "This %s" "doesn'
cprintfn ConsoleColor.Yellow "This %s" "doesn't"
(cprintfn ConsoleColor.Yellow) "still %s" "doesn't"
(cprintfn ConsoleColor.Yellow "still %s") "doesn't"
cprintfn ConsoleColor.Yellow ("still %s" "doesn't")
问题是,在定义使用格式化字符串的函数时,始终需要使用部分应用程序。也就是说,您的功能必须采用以下形式:
cprintf System.ConsoleColor.Red "%s %d" "hi" 42
首先,您的cprintf函数非常好,您可以将其用于可变数量的参数。问题仅在于cprintfn功能:
cprintfn ConsoleColor.Yellow "This %s" "doesn't"
(cprintfn ConsoleColor.Yellow) "still %s" "doesn't"
(cprintfn ConsoleColor.Yellow "still %s") "doesn't"
cprintfn ConsoleColor.Yellow ("still %s" "doesn't")
问题是,在定义使用格式化字符串的函数时,始终需要使用部分应用程序。也就是说,您的功能必须采用以下形式:
cprintf System.ConsoleColor.Red "%s %d" "hi" 42
令人惊叹的但是我想你的意思是说让cprintf c=cprintfw与c和让cprintfn c=cprintfw与\n c。谢谢否则,它们就不会部分适用!但是我想你的意思是说让cprintf c=cprintfw与c和让cprintfn c=cprintfw与\n c。谢谢否则,它们不会部分应用
let myprintf fmt =
<whatever>
otherprintf <whatever> fmt
let cprintfWith endl c fmt =
Printf.kprintf
(fun s ->
let old = System.Console.ForegroundColor
try
System.Console.ForegroundColor <- c;
System.Console.Write (s + endl)
finally
System.Console.ForegroundColor <- old)
fmt
let cprintf c fmt = cprintfWith "" c fmt
let cprintfn c fmt = cprintfWith "\n" c fmt