layout: post
title: “Implementing a builder: Overloading”
description: “Stupid method tricks”
nav: thinking-functionally
seriesId: “Computation Expressions”

seriesOrder: 9

In this post, we’ll take a detour and look at some tricks you can do with methods in a computation expression builder.

Ultimately, this detour will lead to a dead end, but I hope the journey might provide some more insight into good practices for designing your own computation expressions.

An insight: builder methods can be overloaded

At some point, you might have an insight:

  • The builder methods are just normal class methods, and unlike standalone functions, methods can support overloading with different parameter types, which means we can create different implementations of any method, as long as the parameter types are different.

So then you might get excited about this and how it could be used. But it turns out to be less useful than you might think. Let’s look at some examples.

Overloading “return”

Say that you have a union type. You might consider overloading Return or Yield with multiple implementations for each union case.

For example, here’s a very simple example where Return has two overloads:

  1. type SuccessOrError =
  2. | Success of int
  3. | Error of string
  4. type SuccessOrErrorBuilder() =
  5. member this.Bind(m, f) =
  6. match m with
  7. | Success s -> f s
  8. | Error _ -> m
  9. /// overloaded to accept ints
  10. member this.Return(x:int) =
  11. printfn "Return a success %i" x
  12. Success x
  13. /// overloaded to accept strings
  14. member this.Return(x:string) =
  15. printfn "Return an error %s" x
  16. Error x
  17. // make an instance of the workflow
  18. let successOrError = new SuccessOrErrorBuilder()

And here it is in use:

  1. successOrError {
  2. return 42
  3. } |> printfn "Result for success: %A"
  4. // Result for success: Success 42
  5. successOrError {
  6. return "error for step 1"
  7. } |> printfn "Result for error: %A"
  8. //Result for error: Error "error for step 1"

What’s wrong with this, you might think?

Well, first, if we go back to the discussion on wrapper types, we made the point that wrapper types should be generic. Workflows should be reusable as much as possible — why tie the implementation to any particular primitive type?

What that means in this case is that the union type should be resigned to look like this:

  1. type SuccessOrError<'a,'b> =
  2. | Success of 'a
  3. | Error of 'b

But as a consequence of the generics, the Return method can’t be overloaded any more!

Second, it’s probably not a good idea to expose the internals of the type inside the expression like this anyway. The concept of “success” and “failure” cases is useful, but a better way would be to hide the “failure” case and handle it automatically inside Bind, like this:

  1. type SuccessOrError<'a,'b> =
  2. | Success of 'a
  3. | Error of 'b
  4. type SuccessOrErrorBuilder() =
  5. member this.Bind(m, f) =
  6. match m with
  7. | Success s ->
  8. try
  9. f s
  10. with
  11. | e -> Error e.Message
  12. | Error _ -> m
  13. member this.Return(x) =
  14. Success x
  15. // make an instance of the workflow
  16. let successOrError = new SuccessOrErrorBuilder()

In this approach, Return is only used for success, and the failure cases are hidden.

  1. successOrError {
  2. return 42
  3. } |> printfn "Result for success: %A"
  4. successOrError {
  5. let! x = Success 1
  6. return x/0
  7. } |> printfn "Result for error: %A"

We’ll see more of this technique in an upcoming post.

Multiple Combine implementations

Another time when you might be tempted to overload a method is when implementing Combine.

Let’s revisit the Combine method for the trace workflow. If you remember, in the previous implementation of Combine, we just added the numbers together.

But what if we change our requirements, and say that:

  • if we yield multiple values in the trace workflow, then we want to combine them into a list.

A first attempt using combine might look this:

  1. member this.Combine (a,b) =
  2. match a,b with
  3. | Some a', Some b' ->
  4. printfn "combining %A and %A" a' b'
  5. Some [a';b']
  6. | Some a', None ->
  7. printfn "combining %A with None" a'
  8. Some [a']
  9. | None, Some b' ->
  10. printfn "combining None with %A" b'
  11. Some [b']
  12. | None, None ->
  13. printfn "combining None with None"
  14. None

In the Combine method, we unwrap the value from the passed-in option and combine them into a list wrapped in a Some (e.g. Some [a';b']).

For two yields it works as expected:

  1. trace {
  2. yield 1
  3. yield 2
  4. } |> printfn "Result for yield then yield: %A"
  5. // Result for yield then yield: Some [1; 2]

And for a yielding a None, it also works as expected:

  1. trace {
  2. yield 1
  3. yield! None
  4. } |> printfn "Result for yield then None: %A"
  5. // Result for yield then None: Some [1]

But what happens if there are three values to combine? Like this:

  1. trace {
  2. yield 1
  3. yield 2
  4. yield 3
  5. } |> printfn "Result for yield x 3: %A"

If we try this, we get a compiler error:

  1. error FS0001: Type mismatch. Expecting a
  2. int option
  3. but given a
  4. 'a list option
  5. The type 'int' does not match the type ''a list'

What is the problem?

The answer is that after combining the 2nd and 3rd values (yield 2; yield 3), we get an option containing a list of ints or int list option. The error happens when we attempt to combine the first value (Some 1) with the combined value (Some [2;3]). That is, we are passing a int list option as the second parameter of Combine, but the first parameter is still a normal int option. The compiler is telling you that it wants the second parameter to be the same type as the first.

But, here’s where we might want use our overloading trick. We can create two different implementations of Combine, with different types for the second parameter, one that takes an int option and the other taking an int list option.

So here are the two methods, with different parameter types:

  1. /// combine with a list option
  2. member this.Combine (a, listOption) =
  3. match a,listOption with
  4. | Some a', Some list ->
  5. printfn "combining %A and %A" a' list
  6. Some ([a'] @ list)
  7. | Some a', None ->
  8. printfn "combining %A with None" a'
  9. Some [a']
  10. | None, Some list ->
  11. printfn "combining None with %A" list
  12. Some list
  13. | None, None ->
  14. printfn "combining None with None"
  15. None
  16. /// combine with a non-list option
  17. member this.Combine (a,b) =
  18. match a,b with
  19. | Some a', Some b' ->
  20. printfn "combining %A and %A" a' b'
  21. Some [a';b']
  22. | Some a', None ->
  23. printfn "combining %A with None" a'
  24. Some [a']
  25. | None, Some b' ->
  26. printfn "combining None with %A" b'
  27. Some [b']
  28. | None, None ->
  29. printfn "combining None with None"
  30. None

Now if we try combining three results, as before, we get what we expect.

  1. trace {
  2. yield 1
  3. yield 2
  4. yield 3
  5. } |> printfn "Result for yield x 3: %A"
  6. // Result for yield x 3: Some [1; 2; 3]

Unfortunately, this trick has broken some previous code! If you try yielding a None now, you will get a compiler error.

  1. trace {
  2. yield 1
  3. yield! None
  4. } |> printfn "Result for yield then None: %A"

The error is:

  1. error FS0041: A unique overload for method 'Combine' could not be determined based on type information prior to this program point. A type annotation may be needed.

But hold on, before you get too annoyed, try thinking like the compiler. If you were the compiler, and you were given a None, which method would you call?

There is no correct answer, because a None could be passed as the second parameter to either method. The compiler does not know where this is a None of type int list option (the first method) or a None of type int option (the second method).

As the compiler reminds us, a type annotation will help, so let’s give it one. We’ll force the None to be an int option.

  1. trace {
  2. yield 1
  3. let x:int option = None
  4. yield! x
  5. } |> printfn "Result for yield then None: %A"

This is ugly, of course, but in practice might not happen very often.

More importantly, this is a clue that we have a bad design. Sometimes the computation expression returns an 'a option and sometimes it returns an 'a list option. We should be consistent in our design, so that the computation expression always returns the same type, no matter how many yields are in it.

That is, if we do want to allow multiple yields, then we should use 'a list option as the wrapper type to begin with rather than just a plain option. In this case the Yield method would create the list option, and the Combine method could be collapsed to a single method again.

Here’s the code for our third version:

  1. type TraceBuilder() =
  2. member this.Bind(m, f) =
  3. match m with
  4. | None ->
  5. printfn "Binding with None. Exiting."
  6. | Some a ->
  7. printfn "Binding with Some(%A). Continuing" a
  8. Option.bind f m
  9. member this.Zero() =
  10. printfn "Zero"
  11. None
  12. member this.Yield(x) =
  13. printfn "Yield an unwrapped %A as a list option" x
  14. Some [x]
  15. member this.YieldFrom(m) =
  16. printfn "Yield an option (%A) directly" m
  17. m
  18. member this.Combine (a, b) =
  19. match a,b with
  20. | Some a', Some b' ->
  21. printfn "combining %A and %A" a' b'
  22. Some (a' @ b')
  23. | Some a', None ->
  24. printfn "combining %A with None" a'
  25. Some a'
  26. | None, Some b' ->
  27. printfn "combining None with %A" b'
  28. Some b'
  29. | None, None ->
  30. printfn "combining None with None"
  31. None
  32. member this.Delay(f) =
  33. printfn "Delay"
  34. f()
  35. // make an instance of the workflow
  36. let trace = new TraceBuilder()

And now the examples work as expected without any special tricks:

  1. trace {
  2. yield 1
  3. yield 2
  4. } |> printfn "Result for yield then yield: %A"
  5. // Result for yield then yield: Some [1; 2]
  6. trace {
  7. yield 1
  8. yield 2
  9. yield 3
  10. } |> printfn "Result for yield x 3: %A"
  11. // Result for yield x 3: Some [1; 2; 3]
  12. trace {
  13. yield 1
  14. yield! None
  15. } |> printfn "Result for yield then None: %A"
  16. // Result for yield then None: Some [1]

Not only is the code cleaner, but as in the Return example, we have made our code more generic as well, having gone from a specific type (int option) to a more generic type ('a option).

Overloading “For”

One legitimate case where overloading might be needed is the For method. Some possible reasons:

  • You might want to support different kinds of collections (e.g. list and IEnumerable)
  • You might have a more efficient looping implementation for certain kinds of collections.
  • You might have a “wrapped” version of a list (e.g. LazyList) and you want support looping for both unwrapped and wrapped values.

Here’s an example of our list builder that has been extended to support sequences as well as lists:

  1. type ListBuilder() =
  2. member this.Bind(m, f) =
  3. m |> List.collect f
  4. member this.Yield(x) =
  5. printfn "Yield an unwrapped %A as a list" x
  6. [x]
  7. member this.For(m,f) =
  8. printfn "For %A" m
  9. this.Bind(m,f)
  10. member this.For(m:_ seq,f) =
  11. printfn "For %A using seq" m
  12. let m2 = List.ofSeq m
  13. this.Bind(m2,f)
  14. // make an instance of the workflow
  15. let listbuilder = new ListBuilder()

And here is it in use:

  1. listbuilder {
  2. let list = [1..10]
  3. for i in list do yield i
  4. } |> printfn "Result for list: %A"
  5. listbuilder {
  6. let s = seq {1..10}
  7. for i in s do yield i
  8. } |> printfn "Result for seq : %A"

If you comment out the second For method, you will see the “sequence` example will indeed fail to compile. So the overload is needed.

Summary

So we’ve seen that methods can be overloaded if needed, but be careful at jumping to this solution immediately, because having to doing this may be a sign of a weak design.

In the next post, we’ll go back to controlling exactly when the expressions get evaluated, this time using a delay outside the builder.