1

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#?

Jacqueline Nolis
  • 1,457
  • 15
  • 22
  • Does the `Mutate` method have any overloads? – Fyodor Soikin Oct 20 '17 at 05:10
  • 1
    I think it's just got one: (extension) Image.Mutate<'TPixel (requires default constructor and value type and 'TPixel :> IPixel<'TPixel> and 'TPixel :> ValueType)>([] operations: IImageProcessor<'TPixel> []) : unit – Jacqueline Nolis Oct 20 '17 at 05:32
  • 1
    That parameter type looks like an interface, but your C# code is passing a lambda expression. This does not compute. Are you sure that's the only overload? – Fyodor Soikin Oct 20 '17 at 05:46

2 Answers2

5

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)
Tarmil
  • 11,177
  • 30
  • 35
2

You will need to convert your F# lambda into an Action explicitly:

image.Mutate <| Action<_> (fun x -> x.Kodachrome ())
dumetrulo
  • 1,993
  • 9
  • 11