The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

tempusfugit (144) [Avatar] Offline
#1
p.93 "In F#, we can read command line arguments using the standard .NET Environment.GetCommandLineArgs method."

If C# developers are the primary audience then those familiar with console applications would be accustomed to
[pre]
static void Main(string[] args)
[/pre]
so it may be a good time to mention EntryPointAttribute.

The F# 1.9.6.16 Draft Language Specification:
13.1.2 Explicit “Main” Entry Point
http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/spec.html#_Toc234058195

e.g.

[pre]
let writeGraph file =
let lines = List.of_seq (File.ReadAllLines file)
let data = processLines lines
let total = float (countSum data)
let calcPrc num = int ((float num)/total * 100.0)
for (lbl, num) in data do
Console.WriteLine ("{0,-18} - {1,8} ({2}%)", lbl, num, calcPrc( num ))

let execute (args:string[]) =
if args.Length > 0 && (File.Exists args.[0]) then
writeGraph args.[0]
0 // Return 0. This indicates success.
else
printfn "Please specify a path to a valid data CSV file."
-1

// "Debug->Start Without Debugging" keeps console window open after termination.
// "Debug->Start Debugging" does not.
type ConsoleWindowUtil =
// So under DEBUG throw in an additional stop - otherwise bypass method
[<Conditional("DEBUG")>]
static member keepOpen =
printfn "Press any key to continue . . ."
ignore (Console.ReadKey())

[<EntryPoint>]
let main args =
let exitCode = execute args
ConsoleWindowUtil.keepOpen
exitCode

(* Set file path in Visual Studio Solution Explorer Project Properties
-> Debug(Configuration: All Configurations): Start Options: Command Line Arguments

CAVEAT: EntryPoint has to be the "last declaration in the last file in the compilation sequence".
Otherwise - error FS0191: A function labelled with the 'EntryPointAttribute' attribute
must be the last declaration in the last file in the compilation sequence.
*)
[/pre]


Also note that Listing 4.5 (p.90) references "C:Ch03data.csv" even though the discussion has moved to Chapter 4.
Tomas Petricek (160) [Avatar] Offline
#2
Re: 4.3 Creating a Console Application (Suggestion)
Hi,
thanks for the feedback - I added a brief note about the attribute, so that the readers know this is another option.

Tomas