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
5.2.1 Multiple values In F# and C# (p.110)

The entire paragraph after the "F# version using tuples" code is potentially confusing as it relies on the reader recognizing "default value" as the term used in .NET specifications to refer to the value resulting from having "the value's memory initialized to all-bits-zero".

In many contexts the initialized default value is quite useless (e.g. opening a window with zero width and height) - it is highly unlikely in the real world that I want to access "num" when "succ" is false or even ignore "succ" altogether.

When I first read the paragraph, I was thinking of "defaultValue" as in:

Microsoft.Win32.Registry.GetValue :
keyName:string *
valueName:string *
defaultValue:Object
-> Object

So the paragraph seemed confusing and out of context as there is no explicit "defaultValue" in the code. This was further aggravated by the discussion of the "snd" function, which clearly wasn't necessary in this case because "succ" and "num" where already neatly separated by F#'s pattern matching.

At the end of the same paragraph a "very simple function that allows us to specify the default value to use" is promised for the next chapter. However TryParse is not used beyond this chapter (5) and here a missing value is simply represented with Option.None rather than through a specified "defaultValue".
Tomas Petricek (160) [Avatar] Offline
#2
Re: 5.2.1 Multiple values In F# and C# (Clarification)
Hi,
that's a good point. When I was reading the section I also thought it sounds a bit wrong. I think it may be occasionaly useful to ignore the success flag - in some cases, 0 or false are reasonable default values. In that case it may be better to use "snd" and avoid declaring value (succ) which isn't and shouldn't be used anywhere.

Regarding the function - I guess I forgot about this when writing chapter 6. Thanks for pointing that out. I'll remove the reference, because after reading chapter 6 you should know enough to write a function like this:

let defaultParsed defaultValue (succ, value) =
...if succ then value else defaultValue

defaultParsed (-1) (Int32.TryParse(str))

Using the pipelining operator from chapter 6 you could write the same thing like this:

let n = Int32.TryParse(str) |> defaultParsed -1

Thanks!
Tomas