我可以重写此代码以避免F#错误

时间:2019-10-16 06:49:05

标签: f# partial-application first-class-functions

我在F#中有以下代码

实时版本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()   

这是我正在尝试制作原型以与FuncUI的阿瓦隆方言的Elmish方言一起使用的一些真实代码的简化模拟。

问题是当我尝试创建Binder.oneWay的部分应用程序时出现编译错误

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

错误FS0425:一等函数的类型不能包含byrefs

是否有可能在view函数顶部创建Binder.oneWay函数的部分应用程序?或者我已经将自己编码到角落了?

再次提供实时版本

https://repl.it/repls/CarefulGiganticExtraction

0 个答案:

没有答案