Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 包装printf并仍然获取参数_F# - Fatal编程技术网

F# 包装printf并仍然获取参数

F# 包装printf并仍然获取参数,f#,F#,但是 我知道这与Printf.textWritePerformat与简单字符串有关,但即使像在cprintf c fmt:Printf.textWritePerformat中那样为fmt指定类型,我也无法让kprintf部分工作。我读了很多关于日志记录的答案,但我还是不明白。可以使用格式参数的cprintf吗?首先,你的cprintf函数非常好,你可以使用不同数量的参数。问题仅在于cprintfn功能: cprintfn ConsoleColor.Yellow "This %s" "doesn'

但是

我知道这与Printf.textWritePerformat与简单字符串有关,但即使像在cprintf c fmt:Printf.textWritePerformat中那样为fmt指定类型,我也无法让kprintf部分工作。我读了很多关于日志记录的答案,但我还是不明白。可以使用格式参数的cprintf吗?

首先,你的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函数非常好,您可以将其用于可变数量的参数。问题仅在于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