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
Listing 7.2 Error in code listing (page 179)

rc.Top and rc.Left are swapped

Is:
[pre]
{ Left = rc.Top + wspace
Top = rc.Left + hspace
[/pre]

Should be:
[pre]
{ Left = rc.Left + wspace
Top = rc.Top + hspace
[/pre]


Listing 7.12 Suggestion (page 191)

Here would be a good opportunity to introduce Unchecked.defaultof<_>
That is, instead of:
[pre]
let attr(node:XElement, name, def) =
let attr = node.Attribute(XName.Get(name))
if (attr <> null) then attr.Value else def
[/pre]

use something like:
[pre]
let attr(node:XElement, name, def) =
let attr = node.Attribute(XName.Get(name))
let hasValue at = at <> Unchecked.defaultof<XAttribute>
if hasValue attr then attr.Value else def
[/pre]

it would also serve to emphasize F#'s dislike of "null".
Tomas Petricek (160) [Avatar] Offline
#2
Re: Listing 7.2 Error in code listing (page 179)
Hi,
thanks very much for the error report and feedback! The bug in listing 7.2 should be fixed in the next version. Regarding Unechecked.defaultof - I probably wouldn't use it in this case, because when working with .NET types, we can use the 'null' value, so it's easier and arguably also safer to just use 'null'.

F# doesn't like 'null', but it doesn't like 'defaultof' either. In fact 'defaultof' is even more "dangerous" than 'null', because it can be used for assigning 'null' values to references to F# classes, which isn't otherwise possible.

There is some discussion about 'defaultof' in chapter 11 (where we mention that it can be used if you're writing code that can be called from C# - the C# user can give you a null reference in a place where F# compiler doesn't allow you to use 'null' so you can't write "reference <> null" and have to use defaultof instead).

Thanks!
Tomas