How to use Named Arguments in F# - f#

Calling a Dictionary's Add() method using named arguments works in F#.
let d = Dictionary<string, obj>()
d.Add(key = "five", value = 5)
let d2= Dictionary<obj, obj>()
d2.Add(key = "five", value = 5)
d2.Add(key = 5, value = 5)
In Polly's Context class , there is a similar Add() method with 2 overloads:
Add(key: obj, value: obj) : unit
Add(key: string, value: obj) : unit
And I can use it in all of these ways:
let c = Polly.Context()
c.Add("five", 5)
c.Add(5, 5)
c.Add(key = 5, value = 5)
But not this way, it says it can't resolve between the overloads and needs a type annotation.
c.Add(key = "five", value = 5)
Why is that and how can I fix it?

The compiler cannot work out which method to use. This is more common when dealing with C# and OO style in general with F#. I don't mind, keeps me functionally honest.
The most straightforward way to fix your problem now would be to use:
c.Add(key = ("five" :> obj), value = 5)
By casting it there is no ambiguity about what the type is and the compiler is happy.
Alternatively, you could create a function with explicit types and this will help the compiler figure it out as well. If you are using this a lot I suggest this:
let addToPolly (ctx:Polly.Context) (k:obj) (v:obj) =
ctx.Add(k,v)
ignore()
addToPolly c "five" 5
UPDATE: As #brett pointed out I had inferred the answer but not been explicit. Here are some examples where the compiler is happy.
let pollyAddAsString (ctx:Polly.Context) (k:string) (v:obj) = ctx.Add(key = k, value = v) |> ignore
let pollyAddAsObj (ctx:Polly.Context) (k:obj) (v:obj) = ctx.Add(key = k, value = v) |> ignore
pollyAddAsObj c "five" 5
pollyAddAsString c "five" 5
let (k:string,v:obj) = ("five", 5 :> obj)
let (x:obj,y:obj) = ("five" :> obj, 5 :> obj)
c.Add(key = k, value = v)
c.Add(key = x, value = y)

Related

WebSharper: opaque type with equality checking for `===`

I need a datatype that will be completely opaque in F# with equality defined in terms of JS ===. WebSharper manual says that I should override Equals but I can't make it work.
let x : OpaqueType = X<_>
let f (y : OpaqueType) =
if x = y then // this line should be translated to `if (x === y)`
42
else
10
So, what is the correct definition of OpaqueType?
Of course, I can use obj and add an inline function that will do x === y but I'd like to have something more awesome.
I would go for something like this:
module Test =
open IntelliFactory.WebSharper
[<JavaScript>]
let Counter = ref 0
[<Sealed>]
[<JavaScript>]
type T() =
let hash =
incr Counter
Counter.Value
[<JavaScript>]
override this.GetHashCode() =
hash
[<JavaScript>]
override this.Equals(other: obj) =
other ===. this
[<JavaScript>]
let Main () =
let a = T()
let b = T()
JavaScript.Log(a, b, a = b, a = a, hash a, hash b)
Equality and hashing are expected to be consistent by .NET libraries (for example, for use in Dictionary). They are also associated with types through virtual methods, so inlining will not work correctly. The above code gives you a type with reference-like equality semantics.

Make WebSharper generate simple field access

I need a type that will be translated into a plain JS object so that F# field access will be translated into simple JS field access (this is necessary, since the object will be sent through postMessage, so it'll lose all its methods).
Again, I need
let x = a.b
to be translated into
var x = a.b;
without any method calls.
Here is a slightly modified example from the F# Reference:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val a : int
val b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This won't translate with
x: error : Failed to translate property access: b.'
Ok, let's make those vals mutable:
namespace T
open IntelliFactory.WebSharper
[<JavaScript>]
module A =
type MyClass =
val mutable a : int
val mutable b : int
new(a0, b0) = { a = a0; b = b0; }
let myClassObj = new MyClass(35, 22)
let x = myClassObj.b
This will be successfully translated, but… MyClass.New returns an empty object. The question now starts looking much like a bugreport, right? So, back to the question.
Are there any other ways to achieve what I want?
There are additional issues with record-style constructors "{x = y}". I will have to look into this again on F# 3.0, the older F# did not produce sensible quotations for those and we did some partial workarounds in WebSharper. Right now your example breaks. So here is the working code with a static method instead of a constructor:
type MyClass private () =
[<DefaultValue>]
val mutable a : int
[<DefaultValue>]
val mutable b : int
static member Create(a0, b0) =
let c = MyClass()
c.a <- a0
c.b <- b0
c
let test () =
let myClassObj = MyClass.Create(35, 22)
let x = myClassObj.a
let y = myClassObj.b
JavaScript.Log(x, y)
Trivially, a record would also work.
In some cases where you want to go really low-level you can annotate members with the Inline attribute. When this is too much overhead you can use untyped API:
let x = obj ()
x?a <- 1
let y = x?a
JavaScript.Log(x, y)
try this:
type MyClass (a, b) =
member val A = a with get
member val B = b with get
let myClassObj = new MyClass(35, 22)
let x = myClassObj.B

F# type definition with expression

Is it possible to express something like this:
type id = int > 0
I know its not possible to do statically, since this would mean F# has dependent types. In C# I'm used to do this sort of thing with code contracts and get a runtime enforcement. I'm looking for something similiar here.
Thanks
EDIT:
Thank you for all the answers which have various pros and cons. At the monent I'm only using a small subset of F#, a subset of the ocaml core that lends itself easily to program proofs. So no classes.
Contrary to what others said, I would suggest not using classes here, if I understood your problem correctly.
Since the value is immutable, we need applying constraint only once. Any wrapper classes would be an overhead and load GC. Instead, a simple function will do the job:
let inline constrained predicate errormessage value =
if not (predicate value)
then invalidArg "value" errormessage
else value
let positive =
constrained (fun x -> x > 0) "Value must be positive"
let int1 = positive 5 // OK
let int2 = positive -3 // ArgumentException
You can do the same for other types:
let mustBeLong =
constrained (fun (x:string) -> x.Length > 3) "String must be long"
let str1 = mustBeLong "foobar" // OK
let str2 = mustBeLong "baz" // ArgumentException
Using the same within a struct:
type Point2D =
struct
val X: int
val Y: int
new(x: int, y: int) = { X = positive x; Y = positive y }
end
let point1 = Point2D(5, 3) // OK
let point2 = Point2D(5, -2) // ArgumentException
Define it as a union type:
type Id = Id of int
and shadow the constructor with another function:
let Id n =
assert(n > 0)
Id n
In F#, you have to resort to classes and check arguments inside constructors. Other types such as discriminated unions, records and structs have implicit constructors which you can't easily alter.
type Id(i: int) =
do if i <= 0 then
invalidArg "i" "the argument has to be a positive integer"
member x.Value = i
Pattern matching doesn't play nicely with classes. You can remedy the problem using active patterns:
let (|Id|) (id: Id) = id.Value
let id = Id(1)
match id with
| Id 1 -> printfn "matched"
| _ -> printfn "unmatched"
You could create a generic class like so:
type verify<'t>(t:'t,cond) =
let mutable tval = t
let _verify v = if not (cond v) then failwith "bad argument"
do _verify tval
member x.get() = tval
member x.set v =
_verify v
tval <- v
then you can use it with
verify(1,fun t -> t>0)
using .set will recheck the condition.

Is it possible to pass a F# function by Reflection?

Is it possible to pass a F# function by Reflection?
(*in module A*)
type Foo() =
static member bar n = {1..n}
let functionUsingFoobar (x:(int -> #('a seq)) n =
let z = BarFoo.ofSeq (x n)
z.count
(* in module B
here is where I want to pass Foo.bar by reflection*)
let y = functionUsingFoobar Foo.bar 1000
I cannot invoke the member without the args parameter, so partial function application through InvokeMember cannot work.
let foo = new Foo()
let z = foo.GetType().InvokeMember("bar", System.Reflection.BindingFlags.InvokeMethod, null, foo, [|1000|])
(*tried null, [||], [|null|] for args parameter*)
I'm out of ideas how to pass the function by reflection
The problem is that GetMethod returns a MethodInfo, but you need an F# function value. The easiest way to overcome this mismatch is probably to use CreateDelegate to create a .NET delegate from the method, and then treat the Invoke method as a function value of the correct type:
let d =
typeof<Foo>.GetMethod("bar").CreateDelegate(typeof<System.Func<int,seq<int>>>)
:?> System.Func<int,seq<int>>
functionUsingFooBar d.Invoke 1000
If this is what I think you want, it works just fine
type Foo() =
static member bar n = {1..n}
let functionUsingFoobar (x:(int -> #('a seq))) n =
(x n) |> Seq.length
let y = functionUsingFoobar Foo.bar 1000
let foo = new Foo()
let z = fun t -> foo.GetType().InvokeMember("bar", System.Reflection.BindingFlags.InvokeMethod, null, foo, [|t|])

Can F# Quotations be used to create a function applicable to arbitrary F# record types?

Given an F# record:
type R = { X : string ; Y : string }
and two objects:
let a = { X = null ; Y = "##" }
let b = { X = "##" ; Y = null }
and a predicate on strings:
let (!?) : string -> bool = String.IsNullOrWhiteSpace
and a function:
let (-?>) : string -> string -> string = fun x y -> if !? x then y else x
is there a way to use F# quotations to define:
let (><) : R -> R -> R
with behaviour:
let c = a >< b // = { X = a.X -?> b.X ; Y = a.Y -?> b.Y }
in a way that somehow lets (><) work for any arbitrary F# record type, not just for R.
Short: Can quotations be used to generate F# code for a definition of (><) on the fly given an arbitrary record type and a complement function (-?>) applicable to its fields?
If quotations cannot be used, what can?
You could use F# quotations to construct a function for every specific record and then compile it using the quotation compiler available in F# PowerPack. However, as mentioned in the comments, it is definitely easier to use F# reflection:
open Microsoft.FSharp.Reflection
let applyOnFields (recd1:'T) (recd2:'T) f =
let flds1 = FSharpValue.GetRecordFields(recd1)
let flds2 = FSharpValue.GetRecordFields(recd2)
let flds = Array.zip flds1 flds2 |> Array.map f
FSharpValue.MakeRecord(typeof<'T>, flds)
This function takes records, gets their fields dynamically and then applies f to the fields. You can use it to imiplement your operator like this (I'm using a function with a readable name instead):
type R = { X : string ; Y : string }
let a = { X = null ; Y = "##" }
let b = { X = "##" ; Y = null }
let selectNotNull (x:obj, y) =
if String.IsNullOrWhiteSpace (unbox x) then y else x
let c = applyOnFields a b selectNotNull
The solution using Reflection is quite easy to write, but it might be less efficient. It requires running .NET Reflection each time the function applyOnFields is called. You could use quotations to build an AST that represents the function that you could write by hand if you knew the record type. Something like:
let applyOnFields (a:R) (b:R) f = { X = f (a.X, b.X); Y = f (a.Y, b.Y) }
Generating the function using quotations is more difficult, so I won't post a complete sample, but the following example shows at least a part of it:
open Microsoft.FSharp.Quotations
// Get information about fields
let flds = FSharpType.GetRecordFields(typeof<R>) |> List.ofSeq
// Generate two variables to represent the arguments
let aVar = Var.Global("a", typeof<R>)
let bVar = Var.Global("b", typeof<R>)
// For all fields, we want to generate 'f (a.Field, b.Field)` expression
let args = flds |> List.map (fun fld ->
// Create tuple to be used as an argument of 'f'
let arg = Expr.NewTuple [ Expr.PropertyGet(Expr.Var(aVar), fld)
Expr.PropertyGet(Expr.Var(bVar), fld) ]
// Call the function 'f' (which needs to be passed as an input somehow)
Expr.App(???, args)
// Create an expression that builds new record
let body = Expr.NewRecord(typeof<R>, args)
Once you build the right quotation, you can compile it using F# PowerPack. See for example this snippet.

Resources