Skip to content

UsageFSharp

Jay Tuley edited this page · 2 revisions

Introduction

The following are extensions added to F# by using the ImpromptuInteface.FSharp module available on nuget

Defintions

Dynamic Operator

target?Property, target?Property<-value, and target?Method(arg,arg2) allow you to dynamically get and set properties and call methods.

let ex1 = ExpandoObject()
ex1?Test<-"Hi"//Set Dynamic Property
ex1?Test //Get Dynamic Property

Dynamic Invoke Operator

!? prefix operator allows you to invoke a function, lambda, or callable object

let dyn = (fun x y z -> x + y - z) :> obj
let result:int = !?dyn (3,2,1) // result is 4

Dynamic Cast Operator

The >?> infix operator allows you to dynamically convert to type using its implicit operator. The >>?>> infix operator allows you to dynamically convert to a type using its explicit operator.

 let ele = XElement(XName.Get("Test"),"50")
 let conv:int = ele >>?>> typeof<int>//XElement has explicit convert to int

 let ele2 = 100
 let conv2:BigInteger = ele2 >?> typeof<BigInteger>//BigInteger has implicit convert from Int32

Dynamic Add/Remove Events syntax

dynAddAssign(targetObj)?EventProperty <- EventHandler

dynSubtractAssign(pocoObj)?EventProperty <- EventHandler

Dynamic Invoke named arguments syntax

dynArg(value) ? Name

let expando = !?Build<ExpandoObject>.NewObject (
                 dynArg(1) ? One,
                 dynArg(2) ? Two
              )
expando?One //returns 1
expando?Two //returns 2

Sample Code

FSharp version of dynamic MVVM Calculator sample

Unit Tests for the dynamic extensions

Something went wrong with that request. Please try again.