Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Preprocessor Directives Indentation Sensitive in F#?

Tags:

f#

FSI version: 11.0.50727.1

So I was working on an F# shell script and I ran across something that sort of surprised me.

When I did this:

#if INTERACTIVE
    #r "System.Data.dll"
    #r "FSharp.Data.TypeProviders.dll"
    #r "System.Data.Linq.dll"
#endif

open System

I got an FS0010 error when I pasted the block into the FSI. But if I did not indent the #r lines, no FS0010 error. I'm just sort of surprised that preprocessor lines would be indentation sensitive. Is this a compiler issue or is there something else at work here?

like image 222
Onorio Catenacci Avatar asked Oct 30 '12 12:10

Onorio Catenacci


2 Answers

I think the specification and documentation are quite unclear on this topic, but the specification makes a notable distinction between lexical preprocessor directives and compiler directives (see §12.4):

Compiler directives are declarations in non-nested modules or namespace declaration groups in the following form:

# id string ... string

The lexical preprocessor directives #if, #else, #endif and #indent "off" are similar to compiler directives. For details on #if, #else, #endif, see §3.3. The #indent "off" directive is described in §18.4.

My interpretation is that lexical preprocessor directives are actually hanled by some pre-processor before running the main compilation and so the indentation does not matter for these.

On the other hand, directives like #r, #load, #time etc. are processed later by the compiler and so they need to match the usual F# indentation guidelines.

As @unwind says, the documentation states "Indentation is not significant for preprocessor directives", but I think this applies only to the preprocessor directives listed on that documentation page (which are lexical preprocessor directives and not compiler directives).

like image 90
Tomas Petricek Avatar answered Sep 17 '22 15:09

Tomas Petricek


According to the documentation, it must be something else at work:

Indentation is not significant for preprocessor directives.

like image 44
unwind Avatar answered Sep 20 '22 15:09

unwind