1

Following is the function, which I wrote in F# editor and it works as I expected (answer:18).

let quadruple x = x*2
let cal(u:int) = quadruple u + 10;
let result = cal 4

But if I change the order of code, such as

let cal(u:int) = quadruple u + 10;
let quadruple x = x*2
let result = cal 4

I am getting "the value or constructor 'quadrule' not defined". From the error, I presume, the failure is due to a function call before its declaration. For me, it something like an interpreter style.

Question: Why such constrains ? Is this purposeful to keep some compatibility issue ? Or is it because I don't have any module/class defined ?

4
  • 4
    In F#, both compilation and definition order matters. I don't know what the reason for that is (probably type inference wouldn't work without it, or would work poorly), but the functions/modules have to be defined 'above' any other functions and/or modules using them. Same goes for full files that have to be moved 'up' above other files that might be referencing them. Commented Nov 21, 2013 at 13:12
  • 1
    @Nair: Having your dependencies modeled in code and by the structure of your project is a good thing. In C# it's much harder to manage interdependencies, especially with larger projects. Commented Nov 21, 2013 at 15:47
  • @Daniel, I presume, you mean F# Commented Nov 21, 2013 at 16:12
  • 3
    No, I mean in C#, which doesn't enforce dependency order, it's much harder to keep track. I consider F#'s approach an improvement. Commented Nov 21, 2013 at 16:15

1 Answer 1

2

Despite the minimal amount of type declarations required in idiomatic code, F# is a statically strongly typed language. This is possible because of compiler ability to infer types where it can be done unambiguously thru the process known as type inference.

Simplistically, the processing direction that type inference works in F# is one pass top down left to right over the code. This factor affects both intra- and inter-file placement of definitions and usage of F# code elements.

Applying this principle to your second snippet: when compiler first comes over quadruple in the first line there is nothing known about the type of this element based on information parsed so far that would allow to disambiguat it; that's what the error message communicates.

Note: there is a certain deviation from this principle in the area of F# related to OOP, where usage before definition is allowed to some extent, like for Code property usage in a Sample class method Double() below, where it is used prior to the property definition:

type Sample() =
    member this.Double() = this.Code * 2
    member this.Code with get() : int = 3

For the fine-grain details upon type inference workings the best place to check is F# Language Specification.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.