Type and Module Extensions
In all cases when extending types and modules, the extending code must be added/loaded before the code that is to call it. It must also be made available to the calling code by opening/importing the relevant namespaces.
Adding new functions to existing modules and types using Modules
Modules can be used to add new functions to existing Modules and Types.
The new function can then be called as if it was an original member of List.
Adding new methods/properties to existing types
F# allows functions to be added as "members" to types when they are defined (for example, Record Types). However F# also allows new instance members to be added to existing types - even ones declared elsewhere and in other .net languages.
The following example adds a new instance method
Duplicate to all instances of
this is an arbitrarily chosen variable name to use to refer to the instance of the type that is being extended -
x would work just as well, but would perhaps be less self-describing.
It can then be called in the following ways.
This functionality is very similar to Extension Methods in C#.
New properties can also be added to existing types in the same way. They will automatically become properties if the new member takes no arguments.
Adding new static functions to existing types
F# allow existing types to be extended with new static functions.
This new function can be invoked like this:
This feature can mean that rather than having to create "utility" libraries of functions, they can be added to relevant existing types. This can be useful to create more F#-friendly versions of functions that allow features such as currying.