here is my code :
let go thriller =
work.ListPos.Clear()
thriller
work.Thread.Start()
member X.Start() =
work.ListPos.Add <| new Packet( connector.Authorize("admin","1") ) |> go
So it doesn’t work. I used wrapper with parameter like this :
let inconnection thriller =
using <| new SqlConnection(insql) <| fun X -> X.Open(); thriller X;
inconnection <| fun X ->
I can’t use it this way without parameters because I got no X (parameter) but how to create lambda without parameters ?
Thank you.
As far as I can see, you’re trying to implement the “hole in the middle” pattern – that is, run some initialization, then run a function specified by the user and then run some cleanup.
As dahlbyk already pointed out, you need to pass your
gooperation a function of typeunit -> unit. This can be written like this:Aside. Your other example isn’t really idiomatic F# code. The
usingfunction can be replaced with ausekeyword, which is significantly easier to use:More tricky option.
Alternatively, you can also use the
usekeyword for other things than disposing of resources like SQL connections. This could make yourgofunction even nicer (but it depends on your particular case). The idea is that you’ll be able to write:To do this, you need to define
goas a function that returnsIDisposablethat performs the cleanup: