Why is F# printfn not implemented in terms of Console.WriteLine?

喜夏-厌秋 提交于 2019-12-12 13:03:32

问题


I noticed an unexpected behavior when using F# printfn. It seems to break up the format string into chunks and call Console.Write multiple times for each call to printfn. I would expect it to format the entire string and then call Console.WriteLine once.

I noticed this because I am intercepting the standard Console Output using Console.SetOut call with my own TextWriter which tries to prefix every line of output with a time stamp and some additional custom text.

What gives?


回答1:


Here's my guess, for what it's worth:

  1. Compiler does some magic
  2. At runtime, implementer knew that Write will go to a buffered stream, so that performance won't be drastically different from using something like a StringBuilder object and its ToString() override. Maybe performance is even better, since they avoided an object allocation.


来源:https://stackoverflow.com/questions/39459472/why-is-f-printfn-not-implemented-in-terms-of-console-writeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!