f#

F# type constraint with Enum type

房东的猫 提交于 2019-12-12 17:06:19
问题 I would like to write an F# function that takes a generic enum value and, let's say, doubles its underlying integer value. Fortunately, there's a built-in function called int that converts an enum to an integer, so this should be easy, right? Here's my first attempt: let doubler (value : 't when 't : enum<int>) = 2 * (int value) Sadly, this results in the following compiler messages: Program.fs(2,10): warning FS0064: This construct causes code to be less generic than indicated by the type

Why does this point-free F# function behave differently from the non-point-free version?

扶醉桌前 提交于 2019-12-12 16:24:54
问题 Consider the following F#:- type TestClass() = let getValFromMap m k = Map.find k m let addToMap map k i = map |> Map.add k i let mutable someMap : Map<string,int> = Map.empty let getValFromMapPartial key = getValFromMap someMap key let getValFromMapPartialAndTacit = getValFromMap someMap member this.AddThenGet() = someMap <- addToMap someMap "A" 10 let value = getValFromMapPartial "A" printfn "Value from partial = %i" value // prints out let value = getValFromMapPartialAndTacit "A" // throws

How can I get rid of this “Can not be generalised” error?

▼魔方 西西 提交于 2019-12-12 16:19:19
问题 I have this interface declarations type IModel<'value, 'search, 'target when 'target :> IModel<'value, 'search, 'target>> = abstract token: string with get abstract value: 'value with get abstract search: 'search with get abstract GetEmpty: unit -> 'target abstract ReInitWith: #IModel<_, 'search, _> -> 'target type IModelSimple<'value, 'search> = inherit IModel<'value, 'search, IModelSimple<'value, 'search>> abstract Update: ?token:string * ?value: 'value * ?search: 'search -> IModelSimple<

Unexpected behavior with exception handling in async, possible bug?

前提是你 提交于 2019-12-12 16:19:17
问题 I have stumbled upon a problem when calling a nested Async which happens to be null. An exception is raised but it can't be catched with any of the normal exception handling methods Async workflows provide. The following is a simple test which reproduces the problem: [<Test>] let ``Nested async is null with try-with``() = let g(): Async<unit> = Unchecked.defaultof<Async<unit>> let f = async { try do! g() with e -> printf "%A" e } f |> Async.RunSynchronously |> ignore which results in the

Is it possible, with simple F# pattern matching transformations, to ignore unmatched values without a warning?

蹲街弑〆低调 提交于 2019-12-12 16:16:38
问题 So, I previously asked this question: Can someone help me compare using F# over C# in this specific example (IP Address expressions)? I was looking at the posted code and I was wondering if this code could be written without it producing a warning: let [|a;b;c;d|] = s.Split [|'.'|] IP(parseOrParts a, parseOrParts b, parseOrParts c, parseOrParts d) Is it possible to do something for the match _ pattern ot ignore? Without adding in something like Active Patterns? i want to keep the code as

Is there an easy implementation to use a predicate function in a generic F# query

若如初见. 提交于 2019-12-12 16:07:40
问题 I want to use a generic query function like: let filter predicateFn = predicateFn |> fun f s -> query { for x in s do where (f(x)) select x } Where f should be a predicate function. Obviously, this cannot be translated to a sql query. But is there a solution for this problem. I have seen sample solutions for C#. Approach taken from: Web, Cloud & Mobile Solutions with F# by Daniel Mohl. Edit: example to clarify: let userSorter = fun (u:users) -> u.Login let sorted = query { for user in dbConn

How do define an event in F# visible from C#

耗尽温柔 提交于 2019-12-12 15:57:32
问题 Looking at various bits of documentation, the way of defining an event in F# is to do something like type xyz () = let e = new Event<T> member x.something_happened : IEvent<T> = x.Publish Unfortunately, the type of IEvent is really Miscrosoft.FSharp.Control.IEvent<_>, and it is hence difficult to use from within C#. Some articles suggest adding the CLIEvent attribute to member something_happended above but it seems to make no difference as far as its usability from C# without including the F#

Unable to enter text in WPF TextBox in F# FSI

巧了我就是萌 提交于 2019-12-12 15:51:23
问题 In the F# WPF code below, I am unable to enter any text in the text box when I 'show' the WPF window from the F# FSI. Is this something to do with Windows Forms Event loop being used? let txtBox = new TextBox() txtBox.BorderThickness <- Thickness(2.) txtBox.Margin <- Thickness(7.) txtBox.IsReadOnly <- false let wnd = new Window(Title = "Test", Height = 500., Width = 500.) wnd.Content <- txtBox wnd.Show() Based on the answer below by John Palmer , I have updated the code above with the correct

In depth description of statically resolved type parameters

耗尽温柔 提交于 2019-12-12 15:50:04
问题 I'm looking for a very comprehensive review of statically resolved type parameters. What exactly can be done with them, what their limitations are, what are the effects of using them, how they combine with normal type parameters, and how do instance-level inline members work. The specification itself offers very little on the subject, mentioning them only in passing. It says nothing about the ability to have types with such type parameters (e.g. type Example< ^a> ), how exactly the keywords

What does a colon after a tuple but before another type mean within a method signature?

谁都会走 提交于 2019-12-12 15:21:45
问题 What does a colon that's positioned after a tuple but before another type mean within a method signature? Here's the syntax: member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult = Here's the context: type PushController (imp) = inherit ApiController () member this.Post (portalId : string, req : PushRequestDtr) : IHttpActionResult = match imp req with | Success () -> this.Ok () :> _ | Failure (ValidationFailure msg) -> this.BadRequest msg :> _ | Failure