If you see any F# code listing, you will see that almost all of them start with the command #light. Right now, this is a requirement. The first line in any F# file must have #light. After reading about the language, I grokked that it allowed for a shorter syntax than the alternative, #light “off”. Here’s what I’ve been able to figure out so far. From Development in a Blink, I see this:
The #light declaration makes whitespace significant. Allowing the developer to omit certain keywords such as in, ;, begin, and end.
For those of you that have used Python, this makes sense. Indentation gets to have meaning when #light is on. Blocks of code are physically aligned, so indicating where something starts and stops is redundant and, potentially, useless. So, what happens when we turn #light off? Let’s take the Name type developed in a recent post. With #light, the class looks like this:
#light
namespace SomeNamespace
type Name(fname : string) =
let mutable fName = fname
member this.FName with get() = fName
and set(x) = fName <- x
So, what did #light buy us? Well, it meant that we couldn’t write the following, equivalent code:
#light “off”
namespace SomeNamespace
type Name(fname : string) =
class
let mutable fName = fname
member this.FName with get() = fName
and set(x) = fName <- x
end
The difference, in case you missed it, is that class and end HAD to be declared. Furthermore, the indentation/whitespace was no longer significant. This let’s me write code that is harder to visually parse. And yes, I could have indented the code to make it more readable:
#light “off”
namespace SomeNamespace
type Name(fname : string) =
class
let mutable fName = fname
member this.FName with get() = fName
and set(x) = fName <- x
end
I think I’ll be using #light since most examples on the web stick with this syntax. Being the odd man out won’t help me or anyone else who eventually needs to use my code. Good habits start now. #light also let’s me write less code. I have a fairly consistent ratio of lines of code/defects. If I can avoid some little details and lines of code, I will have fewer mistakes.