Reputation: 4101
In the following ML extract (taken from the Effective ML talk), there is a module abbreviation inside a value binding expression. Is it possible to do the equivalent in F#? I know you can do module abbreviations, I am specifically interested if you can do them "inline" like this.
let command =
let default_config = { exit_code = 0; message = None } in
let flags =
let module F = Command.Flag in
[ F.int "-r" (fun cfg v -> { cfg with exit_code = v });
F.string "-m" (fun cfg v -> { cfg with message = v });
]
...
Upvotes: 1
Views: 181
Reputation: 243096
No, this feature is not available in F#. You can only do top-level module abbreviations (as you say) using:
module F = Command.Flag
You can write these in the middle of a source file, but they have to be at the top-level and their scope is always going to be until the end of a file (or until another definition that hides F
). Perhaps if you used this and then had another definition hiding F
, it would have similar effect. For example:
module L = List
[0 .. 9] |> L.map ((*) 2) // Uses functional `List.map`
module L = Seq
[0 .. 9] |> L.map ((*) 2) // Uses lazy `Seq.map`
I agree taht this would be a useful feature in many cases - on the other hand, the F# programming style is sufficiently different from ML, so the advices from Effective ML talk may not directly map to F# programming. If you need to make something a local definition, then the best option would be to define it as an F# object.
Upvotes: 1