How do you use Console.Readline in F#? Unlike Console.Writeline, it isn't being honored when I call it.
你如何在F#中使用Console.Readline?与Console.Writeline不同,我打电话时它并没有被尊重。
3 个解决方案
#1
If you use
如果你使用
let s = Console.ReadLine
you are only building a delegate that points to the ReadLine function. You need to say
您只构建一个指向ReadLine函数的委托。你需要说
let s = Console.ReadLine()
to actually execute the function. This is just like C# syntax, except type inference means you don't get a compiler warning.
实际执行该功能。这就像C#语法一样,除了类型推断意味着你没有得到编译器警告。
#2
What do you mean by "it isn't being honored"? Here's a small console app I've just written in VS2010b1, and it works fine:
你是什么意思“它没有被尊重”?这是我刚刚在VS2010b1中编写的一个小型控制台应用程序,它运行正常:
open System
let line = Console.ReadLine()
Console.WriteLine("You wrote {0}", line)
// Just to make it pause
let unused = Console.ReadLine()
Are you trying to run the code from F# Interactive within Visual Studio? If so, that may be the issue, as Brian's post explains.
您是否尝试在Visual Studio中运行F#Interactive中的代码?如果是这样,那可能是问题,正如Brian的帖子所解释的那样。
However, I haven't seen the same problem when using F# Interactive from the command line. Here's a complete transcript of a session:
但是,从命令行使用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"
Running Brian's looping code from F# Interactive didn't show the same problem.
从F#Interactive运行Brian的循环代码没有显示同样的问题。
Bottom line: It seems like this is broken in F# Interactive in Visual Studio, but not when running interactively from the command line or in a full console app.
结论:在Visual Studio中的F#Interactive中似乎已经破坏了这一点,但在从命令行或完整控制台应用程序中以交互方式运行时却没有。
#3
I don't have a Beta1 box handy, but I know that in the past we've had a bug where ReadLine() would see the background commands that communicate between the interactive UI and the background process that runs your F# code. It may be interesting to investigate what
我没有方便的Beta1盒子,但我知道在过去我们有一个错误,ReadLine()会看到在交互式UI和运行F#代码的后台进程之间进行通信的后台命令。调查什么可能很有意思
let Foo max =
let rec Loop i =
if i < max then
let line = System.Console.ReadLine()
printfn "line = %s" line
Loop (i+1)
Loop 1
Foo 12
prints when you highlight it and 'Send to Interactive'. I think possibly you'll see a few unexpected interesting lines, followed by lines you type into the window.
在突出显示它并“发送到交互式”时打印。我想你可能会看到一些意想不到的有趣线条,然后是你在窗口中输入的线条。
#1
If you use
如果你使用
let s = Console.ReadLine
you are only building a delegate that points to the ReadLine function. You need to say
您只构建一个指向ReadLine函数的委托。你需要说
let s = Console.ReadLine()
to actually execute the function. This is just like C# syntax, except type inference means you don't get a compiler warning.
实际执行该功能。这就像C#语法一样,除了类型推断意味着你没有得到编译器警告。
#2
What do you mean by "it isn't being honored"? Here's a small console app I've just written in VS2010b1, and it works fine:
你是什么意思“它没有被尊重”?这是我刚刚在VS2010b1中编写的一个小型控制台应用程序,它运行正常:
open System
let line = Console.ReadLine()
Console.WriteLine("You wrote {0}", line)
// Just to make it pause
let unused = Console.ReadLine()
Are you trying to run the code from F# Interactive within Visual Studio? If so, that may be the issue, as Brian's post explains.
您是否尝试在Visual Studio中运行F#Interactive中的代码?如果是这样,那可能是问题,正如Brian的帖子所解释的那样。
However, I haven't seen the same problem when using F# Interactive from the command line. Here's a complete transcript of a session:
但是,从命令行使用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"
Running Brian's looping code from F# Interactive didn't show the same problem.
从F#Interactive运行Brian的循环代码没有显示同样的问题。
Bottom line: It seems like this is broken in F# Interactive in Visual Studio, but not when running interactively from the command line or in a full console app.
结论:在Visual Studio中的F#Interactive中似乎已经破坏了这一点,但在从命令行或完整控制台应用程序中以交互方式运行时却没有。
#3
I don't have a Beta1 box handy, but I know that in the past we've had a bug where ReadLine() would see the background commands that communicate between the interactive UI and the background process that runs your F# code. It may be interesting to investigate what
我没有方便的Beta1盒子,但我知道在过去我们有一个错误,ReadLine()会看到在交互式UI和运行F#代码的后台进程之间进行通信的后台命令。调查什么可能很有意思
let Foo max =
let rec Loop i =
if i < max then
let line = System.Console.ReadLine()
printfn "line = %s" line
Loop (i+1)
Loop 1
Foo 12
prints when you highlight it and 'Send to Interactive'. I think possibly you'll see a few unexpected interesting lines, followed by lines you type into the window.
在突出显示它并“发送到交互式”时打印。我想你可能会看到一些意想不到的有趣线条,然后是你在窗口中输入的线条。