F Sharp Operator Overloading - F Sharp (programming language)

What is F# - Operator Overloading?

You can redefine or overload most of the built-in operators available in F#. Thus a programmer can use operators with user-defined types as well.

Operators are functions with special names, enclosed in brackets. They must be defined as static class members. Like any other function, an overloaded operator has a return type and a parameter list.

The following example, shows a + operator on complex numbers −

The above function implements the addition operator (+) for a user-defined class Complex. It adds the attributes of two objects and returns the resultant Complex object.

Implementation of Operator Overloading

The following program shows the complete implementation −

On compiling and running, we get the below output

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

F Sharp (programming language) Topics