Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Console.Readline work but Console.Readline() does?

Tags:

console

f#

How do you use Console.Readline in F#? Unlike Console.Writeline, it isn't being honored when I call it.

like image 912
Jonathan Allen Avatar asked Jun 04 '09 05:06

Jonathan Allen


2 Answers

If you use

let s = Console.ReadLine

you are only building a delegate that points to the ReadLine function. You need to say

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.

like image 188
Jonathan Allen Avatar answered Oct 12 '22 00:10

Jonathan Allen


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:

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.

However, I haven't seen the same problem when using F# Interactive from the command line. Here's a complete transcript of a session:

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 [email protected]
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.

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.

like image 22
Jon Skeet Avatar answered Oct 12 '22 00:10

Jon Skeet