Chapter 9. Turning values into F# object types with members

 

This chapter covers

  • Declaring types with members
  • Using interfaces in a functional way
  • Writing class and interface types
  • Using F# code from C#

When we introduced F# in chapter 1, we described it as a multiparadigm language that takes the best elements of several worlds. Most importantly, it takes ideas from both functional and object-oriented languages. In this chapter, we’re going to look at several features inspired by OOP or that allow fluent integration with object-oriented .NET languages like C# and Visual Basic .NET (VB.NET).

This chapter is particularly important for the later steps in the F# development process. As we’ve mentioned, functional simplicity allows us to write a program quickly and provides great flexibility. OOP in F# is valuable because it gives the code a solid structure, encapsulates related functionality, and allows painless integration with other systems or .NET programming languages. In this chapter, we’ll see how to take F# code that we developed earlier and evolve it to make it easier to use in a team or in a larger project.

9.1. Improving data-centric applications

9.1.1. Adding members to F# types

9.1.2. Appending members using type extensions

9.2. Improving behavior-centric applications

9.2.1. Using records of functions

9.2.2. Using interface object types

9.3. Working with .NET interfaces

9.3.1. Using .NET collections

9.3.2. Cleaning resources using IDisposable

9.4. Concrete object types

9.4.1. Functional and imperative classes

9.4.2. Implementing interfaces and casting

9.5. Using F# libraries from C#

9.5.1. Working with records and members

9.5.2. Working with values and delegates

9.6. Summary