你如何在F#中使用Console.Readline?与Console.Writeline不同,我打电话时它并没有被尊重.
Jon*_*len 29
如果你使用
let s = Console.ReadLine
Run Code Online (Sandbox Code Playgroud)
您只构建一个指向ReadLine函数的委托.你需要说
let s = Console.ReadLine()
Run Code Online (Sandbox Code Playgroud)
实际执行该功能.这就像C#语法一样,除了类型推断意味着你没有得到编译器警告.
你是什么意思"它没有被尊重"?这是我刚刚在VS2010b1中编写的一个小型控制台应用程序,它运行正常:
open System
let line = Console.ReadLine()
Console.WriteLine("You wrote {0}", line)
// Just to make it pause
let unused = Console.ReadLine()
Run Code Online (Sandbox Code Playgroud)
您是否尝试在Visual Studio中运行F#Interactive的代码?如果是这样,那可能就是问题,正如Brian的帖子所解释的那样.
但是,从命令行使用F#Interactive时,我没有看到同样的问题.这是一个完整的会话记录:
Microsoft F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.9.6.16, compiling for .NET Framework Version v4.0.20506
Please send bug reports to fsbugs@microsoft.com
For help type #help;;
> open System;;
> let line = Console.ReadLine();;
Hello world
val line : string = "Hello world"
Run Code Online (Sandbox Code Playgroud)
从F#Interactive运行Brian的循环代码没有显示同样的问题.
结论:在Visual Studio中的F#Interactive中似乎已经破坏了这一点,但在从命令行或完整控制台应用程序中以交互方式运行时却没有.