3

To use Hangfire in combination with F# I require a little help:

Hangfire's BackgroundJob.Enqueue takes as a parameter an System.Linq.Expressions.Expression with generic type Action<'T>.

So in full: Expression<Action<'T>>

And on my side I've got a method to my disposal:

let doSomething () = () Thus its signature is unit -> unit.

How I could perfectly pass in Action(doSomething) if the callee took an Action, but it does not.

So how do I convert my unit -> unit to a Expression<Action<'T>>?

svick
  • 236,525
  • 50
  • 385
  • 514
Anemoia
  • 7,928
  • 7
  • 46
  • 71
  • 2
    Converting `someFunc` of type `unit -> unit` into `Action` is as simple as `Action(someFunc)`. However, converting a function into a LINQ expression is a bit more difficult. See [this answer to a different question](http://stackoverflow.com/a/23146624/24380) for an example that might be of help. – Joel Mueller May 02 '16 at 21:23
  • 2
    There's a type-directed conversion from syntactic F# functions to `Expression<_>`s when used as method arguments, so try just calling `Enqueue(fun () -> doSomething())`. – kvb May 03 '16 at 01:15

1 Answers1

5

As kvb said in a comment, F# supports converting lambdas to Expressions, just like C#. So, if you want to create an expression that ignores its input and invokes doSomething, it's just:

BackgroundJob.Enqueue(fun x -> doSomething())
svick
  • 236,525
  • 50
  • 385
  • 514