O'Reilly logo

Programming F# by Chris Smith

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Applied Object-Oriented Programming

This chapter could also be called Being Functional in an Object-Oriented World. By now, you’ve mastered the F# syntax and style, but there is still plenty to be said about using F# in the object-oriented landscape of the .NET platform.

In this chapter, you will learn how to create F# types that integrate better with other object-oriented languages like C# when consumed: for example, adding operator overloading to allow your types to be used with symbolic code as well as adding events to your code to allow consumers to receive notifications when certain actions occur. This not only enables you to be more expressive in your F# code, but it also reduces the friction with non-F# developers when sharing your code.

Operators

Using a symbolic notation for manipulating objects can simplify your code by eliminating the need to call methods like Add and Remove, instead using more intuitive counterparts like + and -. Also, if an object represents a collection of data, being able to index directly into it with the .[ ] or .[expr .. expr] notation can make it much easier to access the data an object holds.

Operator Overloading

Operator overloading is a term used for adding new meaning to an existing operator, like + or -. Instead of only working on primitive types, you can overload those operators to accept custom types you create. Operator overloading in F# can be done by simply adding a static method to a type. After that, you can use the operator ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required