Using the required qualified access attribute in F# to avoid name clashes
April 15, 2017 Leave a comment
It can happen that a number of different modules have the same function name but have a different implementation. Here’s a module with a single function called “sum”:
module Top let sum x y = x + y
If “sum” is likely to reoccur as a function name then it’s possible to require that the caller call sum by its fully qualified name, i.e. Top.sum, using the RequireQualifiedAccessAttribute attribute as follows:
[<RequireQualifiedAccessAttribute>] module Top let sum x y = x + y
Here’s how to call the sum function from a different part of the application:
let qualifSum = Top.sum 10 20
If we try to reference the Top module with the “open” statement…
…then we’ll get a compiler error:
This declaration opens the module ‘Top’, which is marked as ‘RequireQualifiedAccess’. Adjust your code to use qualified references to the elements of the module instead, e.g. ‘List.map’ instead of ‘map’. This change will ensure that your code is robust as new constructs are added to libraries.
View all F# related articles here.