32

I'm in the process of learning F# and am enjoying it so far. Almost all of the examples online use the lightweight syntax (#light); however, also give a comment about it being on for said example in most cases.

Is it better to learn F# using #light enabled or disabled? I'm planning on eventually learning it w/o it turned on but am curious on if it would be better to learn it at the beginning or work on applying it after I know the core language more.

JamesEggers
  • 12,885
  • 14
  • 59
  • 86

4 Answers4

37

I'd definitely prefer learning F# with the #light syntax. The non-light version is sometimes useful for understanding some tricks about the F# syntax, but the #light syntax gives you much pleasant experience.

For example - using #light

let add a b c = 
  let ab = a + b
  printfn "%d" ab
  c - ab

Using non-light you can write the same thing like this:

let add a b c = 
  let ab = a + b in // 'in' keyword specifies where the binding (value 'ab') is valid
  printfn "%d" ab;  // ';' is operator for sequencing expressions
  c - ab;;          // ';;' is end of a function declaration

This for example shows that you cannot write something like:

let doNothing a b = 
  let sum = a + b in 

There is an 'in' keyword at the end but the function doesn't have any body (because there is no expression following 'in'). In this case non-light syntax is sometimes interesting to understand what's going on... But as you can see, the #light code is a lot simpler.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 2
    Indentation, or parentheses, or ... there's lots of ways to communicate scope other than 'in' or ';' or other delimiter tokens. After getting past the initial-recoil-reaction, most people find that significant whitespace is great. – Brian Jan 21 '09 at 02:05
  • 2
    @Brian: Unless you are blind, in which case programming in a language where whitespace is important is quite tedious: http://stackoverflow.com/questions/118643/ – Tamas Czinege Jan 21 '09 at 11:11
  • Thanks for the example. I agree that whitespace can be tedious, I haven't found the whitespace from #light to be daunting unlike other some other languages. From this answer, I've decided to focus on #light to learn the language and then apply the more verbose syntax once I'm more familiar with F#. – JamesEggers Jan 21 '09 at 12:50
  • @DrJokepu: I think unless the compiler can parse the code there shouldn't be any problem. If you're blind, you'd probably prefer to work with "AST" rather than the source code as a text... allowing blind people to code more easily would be quite interesting usability problem I guess. – Tomas Petricek Jan 22 '09 at 02:09
  • @nlucaroni: If the code is well written then the scope is easy to see. If it's poorly written, then additional keyword will not save us. If a line is too long then it's probably a good idea to split it into two. BTW: I use non-light symbols occasionaly when a line get's long too :-) – Tomas Petricek Jan 22 '09 at 02:11
11

The "#light" will probably become the default in a future release of the language, so I would learn it that way. I think it's rare for anyone to use the heavier syntax except for OCaml-compatibility (either when cross-compiling, or because the human sitting at the keyboard knows OCaml and is making a smoother transition to F#).

Brian
  • 117,631
  • 17
  • 236
  • 300
7

Because I learned F# from an OCaml book (and I use an OCaml mode for Emacs to edit F# code), I prefer to use the "heavy" syntax. I have worked with #light code, and of course most of the F# examples are written using the light syntax so having some general familiarity is useful. That said, it's quite a bit easier to switch from heavy to light than the other way around, so it's certainly not a bad idea to learn it using the heavy syntax.

I have come across the occasional annoying bug with heavy syntax being treated as a second class citizen (combine was broken for computation expressions a couple releases back), but these are pretty rare. Generally speaking, I don't think the differences are very significant and I need to look close to determine which syntax is being used when looking at code in isolation. YMMV.

Derek Slager
  • 13,619
  • 3
  • 34
  • 34
1

If I remember correctly, book "Expert C#" mentions that #light will be the default when F# ships and that non-light syntax is intended for compatibility only.

bh213
  • 6,343
  • 9
  • 43
  • 52