Can I rewrite this code to avoid the F# error

谁说胖子不能爱 提交于 2019-12-11 04:18:55

问题


I have the following code in F#

live version at https://repl.it/repls/CarefulGiganticExtraction

let inline tryParse text = 
    let mutable r = Unchecked.defaultof<_>
    (^a : (static member TryParse: string * ^a byref -> bool) (text, &r)), r 

let inline tryParseWithDefault (defaultVal:'a) text =
    match tryParse text with 
    | true, v -> v
    | _ -> defaultVal

type TextBox(text:string) =
  member this.Text = text

type View() =
   member this.Name = "View"

type State = { count: int }


module Binder =
    let inline oneWay defaultValue update dispatch (sender:obj) _ =
        let value = 
            match sender with 
            | :? TextBox as textBox -> tryParseWithDefault defaultValue (textBox.Text) 
            | _ -> defaultValue
        dispatch (fun state -> update state value ) 

let view (state: State) (dispatch): View =
    // Create a partially applied oneWay binding function
    // This fails to compile due to 
    // "The type of a first class function cannot contain byrefs"
    let inline oneWay defaultValue update = 
        Binder.oneWay defaultValue update dispatch 

    // Return the mock view
    View()   

This is a cut down mock of some real code I'm trying to prototype to work with FuncUI an Elmish dialect for Avalonia.

The problem is that I get the compile error when I try to create the partial application of Binder.oneWay

let inline oneWay defaultValue update = 
    Binder.oneWay defaultValue update dispatch

error FS0425: The type of a first-class function cannot contain byrefs

Is it possible to create a partial application of the Binder.oneWay function at the top of the view function or have I coded myself into a corner?

Again the live version is at

https://repl.it/repls/CarefulGiganticExtraction

来源:https://stackoverflow.com/questions/58407301/can-i-rewrite-this-code-to-avoid-the-f-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!