Search code examples
f#imagesharp

Imagesharp mutate in F#


I am trying to use ImageSharp to edit pictures in F#. I'm struggling to get the image mutations working

To do an image mutation in C#, it looks like you just use the mutate method and a lambda:

image.Mutate(x => x.Kodachrome())

Normally, to change C# lambdas to F#, I would just use anonymous functions, like so:

image.Mutate(fun x -> x.Kodachrome())

When I do so, I get the following error:

No overloads match for method 'Mutate'. The available overloads are shown below (or in the Error List window).

It looks like the Mutate method takes an ImageProcessor, but for some reason in F# the compiler can't figure out that the anonymous function is an ImageProcessor. How can I get an image mutation to work in F#?


Solution

  • F# can automatically convert from an anonymous function (fun ...) to a System.Action<_>, but only if the types match exactly. Here, they don't, because Kodachrome() doesn't return unit. So you need to ignore its return value:

    image.Mutate(fun x -> x.Kodachrome() |> ignore)