layout: post
title: “Attaching functions to types”
description: “Creating methods the F# way”
nav: thinking-functionally
seriesId: “Thinking functionally”

seriesOrder: 11

Although we have focused on the pure functional style so far, sometimes it is convenient to switch to an object oriented style.
And one of the key features of the OO style is the ability to attach functions to a class and “dot into” the class to get the desired behavior.

In F#, this is done using a feature called “type extensions”. And any F# type, not just classes, can have functions attached to them.

Here’s an example of attaching a function to a record type.

  1. module Person =
  2. type T = {First:string; Last:string} with
  3. // member defined with type declaration
  4. member this.FullName =
  5. this.First + " " + this.Last
  6. // constructor
  7. let create first last =
  8. {First=first; Last=last}
  9. // test
  10. let person = Person.create "John" "Doe"
  11. let fullname = person.FullName

The key things to note are:

  • The with keyword indicates the start of the list of members
  • The member keyword shows that this is a member function (i.e. a method)
  • The word this is a placeholder for the object that is being dotted into (called a “self-identifier”). The placeholder prefixes the function name, and then the function body then uses the same placeholder when it needs to refer to the current instance.
    There is no requirement to use a particular word, just as long as it is consistent. You could use this or self or me or any other word that commonly indicates a self reference.

You don’t have to add a member at the same time that you declare the type, you can always add it later in the same module:

  1. module Person =
  2. type T = {First:string; Last:string} with
  3. // member defined with type declaration
  4. member this.FullName =
  5. this.First + " " + this.Last
  6. // constructor
  7. let create first last =
  8. {First=first; Last=last}
  9. // another member added later
  10. type T with
  11. member this.SortableName =
  12. this.Last + ", " + this.First
  13. // test
  14. let person = Person.create "John" "Doe"
  15. let fullname = person.FullName
  16. let sortableName = person.SortableName

These examples demonstrate what are called “intrinsic extensions”. They are compiled into the type itself and are always available whenever the type is used. They also show up when you use reflection.

With intrinsic extensions, it is even possible to have a type definition that divided across several files, as long as all the components use the same namespace and are all compiled into the same assembly.
Just as with partial classes in C#, this can be useful to separate generated code from authored code.

Optional extensions

Another alternative is that you can add an extra member from a completely different module.
These are called “optional extensions”. They are not compiled into the type itself, and require some other module to be in scope for them to work (this behavior is just like C# extension methods).

For example, let’s say we have a Person type defined:

  1. module Person =
  2. type T = {First:string; Last:string} with
  3. // member defined with type declaration
  4. member this.FullName =
  5. this.First + " " + this.Last
  6. // constructor
  7. let create first last =
  8. {First=first; Last=last}
  9. // another member added later
  10. type T with
  11. member this.SortableName =
  12. this.Last + ", " + this.First

The example below demonstrates how to add an UppercaseName extension to it in a different module:

  1. // in a different module
  2. module PersonExtensions =
  3. type Person.T with
  4. member this.UppercaseName =
  5. this.FullName.ToUpper()

So now let’s test this extension:

  1. let person = Person.create "John" "Doe"
  2. let uppercaseName = person.UppercaseName

Uh-oh, we have an error. What’s wrong is that the PersonExtensions is not in scope.
Just as for C#, any extensions have to be brought into scope in order to be used.

Once we do that, everything is fine:

  1. // bring the extension into scope first!
  2. open PersonExtensions
  3. let person = Person.create "John" "Doe"
  4. let uppercaseName = person.UppercaseName

Extending system types

You can extend types that are in the .NET libraries as well. But be aware that when extending a type, you must use the actual type name, not a type abbreviation.

For example, if you try to extend int, you will fail, because int is not the true name of the type:

  1. type int with
  2. member this.IsEven = this % 2 = 0

You must use System.Int32 instead:

  1. type System.Int32 with
  2. member this.IsEven = this % 2 = 0
  3. //test
  4. let i = 20
  5. if i.IsEven then printfn "'%i' is even" i

Static members

You can make the member functions static by:

  • adding the keyword static
  • dropping the this placeholder
  1. module Person =
  2. type T = {First:string; Last:string} with
  3. // member defined with type declaration
  4. member this.FullName =
  5. this.First + " " + this.Last
  6. // static constructor
  7. static member Create first last =
  8. {First=first; Last=last}
  9. // test
  10. let person = Person.T.Create "John" "Doe"
  11. let fullname = person.FullName

And you can create static members for system types as well:

  1. type System.Int32 with
  2. static member IsOdd x = x % 2 = 1
  3. type System.Double with
  4. static member Pi = 3.141
  5. //test
  6. let result = System.Int32.IsOdd 20
  7. let pi = System.Double.Pi

Attaching existing functions

A very common pattern is to attach pre-existing standalone functions to a type. This has a couple of benefits:

  • While developing, you can create standalone functions that refer to other standalone functions. This makes programming easier because type inference works much better with functional-style code than with OO-style (“dotting into”) code.
  • But for certain key functions, you can attach them to the type as well. This gives clients the choice of whether to use functional or object-oriented style.

One example of this in the F# libraries is the function that calculates a list’s length. It is available as a standalone function in the List module, but also as a method on a list instance.

  1. let list = [1..10]
  2. // functional style
  3. let len1 = List.length list
  4. // OO style
  5. let len2 = list.Length

In the following example, we start with a type with no members initially, then define some functions, then finally attach the fullName function to the type.

  1. module Person =
  2. // type with no members initially
  3. type T = {First:string; Last:string}
  4. // constructor
  5. let create first last =
  6. {First=first; Last=last}
  7. // standalone function
  8. let fullName {First=first; Last=last} =
  9. first + " " + last
  10. // attach preexisting function as a member
  11. type T with
  12. member this.FullName = fullName this
  13. // test
  14. let person = Person.create "John" "Doe"
  15. let fullname = Person.fullName person // functional style
  16. let fullname2 = person.FullName // OO style

The standalone fullName function has one parameter, the person. In the attached member, the parameter comes from the this self-reference.

Attaching existing functions with multiple parameters

One nice thing is that when the previously defined function has multiple parameters, you don’t have to respecify them all when doing the attachment, as long as the this parameter is first.

In the example below, the hasSameFirstAndLastName function has three parameters. Yet when we attach it, we only need to specify one!

  1. module Person =
  2. // type with no members initially
  3. type T = {First:string; Last:string}
  4. // constructor
  5. let create first last =
  6. {First=first; Last=last}
  7. // standalone function
  8. let hasSameFirstAndLastName (person:T) otherFirst otherLast =
  9. person.First = otherFirst && person.Last = otherLast
  10. // attach preexisting function as a member
  11. type T with
  12. member this.HasSameFirstAndLastName = hasSameFirstAndLastName this
  13. // test
  14. let person = Person.create "John" "Doe"
  15. let result1 = Person.hasSameFirstAndLastName person "bob" "smith" // functional style
  16. let result2 = person.HasSameFirstAndLastName "bob" "smith" // OO style

Why does this work? Hint: think about currying and partial application!

Tuple-form methods

When we start having methods with more than one parameter, we have to make a decision:

  • we could use the standard (curried) form, where parameters are separated with spaces, and partial application is supported.
  • we could pass in all the parameters at once, comma-separated, in a single tuple.

The “curried” form is more functional, and the “tuple” form is more object-oriented.

The tuple form is also how F# interacts with the standard .NET libraries, so let’s examine this approach in more detail.

As a testbed, here is a Product type with two methods, each implemented using one of the approaches.
The CurriedTotal and TupleTotal methods each do the same thing: work out the total price for a given quantity and discount.

  1. type Product = {SKU:string; Price: float} with
  2. // curried style
  3. member this.CurriedTotal qty discount =
  4. (this.Price * float qty) - discount
  5. // tuple style
  6. member this.TupleTotal(qty,discount) =
  7. (this.Price * float qty) - discount

And here’s some test code:

  1. let product = {SKU="ABC"; Price=2.0}
  2. let total1 = product.CurriedTotal 10 1.0
  3. let total2 = product.TupleTotal(10,1.0)

No difference so far.

We know that curried version can be partially applied:

  1. let totalFor10 = product.CurriedTotal 10
  2. let discounts = [1.0..5.0]
  3. let totalForDifferentDiscounts
  4. = discounts |> List.map totalFor10

But the tuple approach can do a few things that that the curried one can’t, namely:

  • Named parameters
  • Optional parameters
  • Overloading

Named parameters with tuple-style parameters

The tuple-style approach supports named parameters:

  1. let product = {SKU="ABC"; Price=2.0}
  2. let total3 = product.TupleTotal(qty=10,discount=1.0)
  3. let total4 = product.TupleTotal(discount=1.0, qty=10)

As you can see, when names are used, the parameter order can be changed.

Note: if some parameters are named and some are not, the named ones must always be last.

Optional parameters with tuple-style parameters

For tuple-style methods, you can specify an optional parameter by prefixing the parameter name with a question mark.

  • If the parameter is set, it comes through as Some value
  • If the parameter is not set, it comes through as None

Here’s an example:

  1. type Product = {SKU:string; Price: float} with
  2. // optional discount
  3. member this.TupleTotal2(qty,?discount) =
  4. let extPrice = this.Price * float qty
  5. match discount with
  6. | None -> extPrice
  7. | Some discount -> extPrice - discount

And here’s a test:

  1. let product = {SKU="ABC"; Price=2.0}
  2. // discount not specified
  3. let total1 = product.TupleTotal2(10)
  4. // discount specified
  5. let total2 = product.TupleTotal2(10,1.0)

This explicit matching of the None and Some can be tedious, and there is a slightly more elegant solution for handling optional parameters.

There is a function defaultArg which takes the parameter as the first argument and a default for the second argument. If the parameter is set, the value is returned.
And if not, the default value is returned.

Let’s see the same code rewritten to use defaultArg

  1. type Product = {SKU:string; Price: float} with
  2. // optional discount
  3. member this.TupleTotal2(qty,?discount) =
  4. let extPrice = this.Price * float qty
  5. let discount = defaultArg discount 0.0
  6. //return
  7. extPrice - discount

Method overloading

In C#, you can have multiple methods with the same name that differ only in their function signature (e.g. different parameter types and/or number of parameters)

In the pure functional model, that does not make sense — a function works with a particular domain type and a particular range type.
The same function cannot work with different domains and ranges.

However, F# does support method overloading, but only for methods (that is functions attached to types) and of these, only those using tuple-style parameter passing.

Here’s an example, with yet another variant on the TupleTotal method!

  1. type Product = {SKU:string; Price: float} with
  2. // no discount
  3. member this.TupleTotal3(qty) =
  4. printfn "using non-discount method"
  5. this.Price * float qty
  6. // with discount
  7. member this.TupleTotal3(qty, discount) =
  8. printfn "using discount method"
  9. (this.Price * float qty) - discount

Normally, the F# compiler would complain that there are two methods with the same name, but in this case, because they are tuple based and because their signatures are different, it is acceptable.
(To make it obvious which one is being called, I have added a small debugging message.)

And here’s a test:

  1. let product = {SKU="ABC"; Price=2.0}
  2. // discount not specified
  3. let total1 = product.TupleTotal3(10)
  4. // discount specified
  5. let total2 = product.TupleTotal3(10,1.0)

Hey! Not so fast… The downsides of using methods

If you are coming from an object-oriented background, you might be tempted to use methods everywhere, because that is what you are familiar with.
But be aware that there some major downsides to using methods as well:

  • Methods don’t play well with type inference
  • Methods don’t play well with higher order functions

In fact, by overusing methods you would be needlessly bypassing the most powerful and useful aspects of programming in F#.

Let’s see what I mean.

Methods don’t play well with type inference

Let’s go back to our Person example again, the one that had the same logic implemented both as a standalone function and as a method:

  1. module Person =
  2. // type with no members initially
  3. type T = {First:string; Last:string}
  4. // constructor
  5. let create first last =
  6. {First=first; Last=last}
  7. // standalone function
  8. let fullName {First=first; Last=last} =
  9. first + " " + last
  10. // function as a member
  11. type T with
  12. member this.FullName = fullName this

Now let’s see how well each one works with type inference. Say that I want to print the full name of a person, so I will define a function printFullName that takes a person as a parameter.

Here’s the code using the module level standalone function.

  1. open Person
  2. // using standalone function
  3. let printFullName person =
  4. printfn "Name is %s" (fullName person)
  5. // type inference worked:
  6. // val printFullName : Person.T -> unit

This compiles without problems, and the type inference has correctly deduced that parameter was a person

Now let’s try the “dotted” version:

  1. open Person
  2. // using method with "dotting into"
  3. let printFullName2 person =
  4. printfn "Name is %s" (person.FullName)

This does not compile at all, because the type inference does not have enough information to deduce the parameter. Any object might implement .FullName — there is just not enough to go on.

Yes, we could annotate the function with the parameter type, but that defeats the whole purpose of type inference.

Methods don’t play well with higher order functions

A similar problem happens with higher order functions. For example, let’s say that, given a list of people, we want to get all their full names.

With a standalone function, this is trivial:

  1. open Person
  2. let list = [
  3. Person.create "Andy" "Anderson";
  4. Person.create "John" "Johnson";
  5. Person.create "Jack" "Jackson"]
  6. //get all the full names at once
  7. list |> List.map fullName

With object methods, we have to create special lambdas everywhere:

  1. open Person
  2. let list = [
  3. Person.create "Andy" "Anderson";
  4. Person.create "John" "Johnson";
  5. Person.create "Jack" "Jackson"]
  6. //get all the full names at once
  7. list |> List.map (fun p -> p.FullName)

And this is just a simple example. Object methods don’t compose well, are hard to pipe, and so on.

So, a plea for those of you new to functionally programming. Don’t use methods at all if you can, especially when you are learning.
They are a crutch that will stop you getting the full benefit from functional programming.