Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# WPF MouseMove params

canvas.MouseMove.Add(move canvas update)

MouseMove.Add( p1 p2 p3)

Usually I see this use and documentation, two params -- (object sender, MouseEventArgs e) -- which I take to be move and canvas in this example code, taken from F#.NET Journal's Computational geometry: quick hull.

Is update some delagate? or routing bubble info to MouseMove.Add?

I'm just not gettng it. Any help welcome. Thanks. Art

like image 613
Art Scott Avatar asked Jan 24 '26 06:01

Art Scott


1 Answers

The answer from kvb gives all the theory, so I thought I could also add a code sample. I don't know the source of the snippet, so I'll make some guesses based on the names - hopefully it will be useful even if it may not be completely same as the original sample.

As kvb says, the code move canvas update is actually a function call that returns a function, which is then added as a handler to the MouseMove event.

This means that move could be declared as follows:

let move (canvas:Canvas) (update:unit -> unit) (me:MouseEventArgs) =
  // This function gets called when the mouse moves
  // - values me.X and me.Y give the current mouse location
  // - we can access 'canvas' that was declared when registering handler
  // - we can call 'update' to do some more work...
  // Pseudo-example:
  canvas.Children.[0].Width <- me.X
  update()

When registering the event handler, the code move canvas update specifies the first two arguments of the move function, so that the handler can access canvas value that was probably declared in the place where the handler is registered (without the use of mutable variables!)

let canvas = new Canvas() // Create canvas
let update () = ... // some function that performs update

// Register handler and give it canvas and update as first two arguments
canvas.MouseMove.Add(move canvas update) 

This should also explain why the event handler does not need to take sender:object as the first argument - you can pass the canvas (which is the sender) as a first argument using partial function application in a statically typed way (so you don't have to cast object to Canvas).

like image 111
Tomas Petricek Avatar answered Jan 25 '26 20:01

Tomas Petricek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!