1

我在 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 进行原型制作,这是Avalonia的 Elmish 方言。

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

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

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

是否可以在视图函数的顶部创建 Binder.oneWay 函数的部分应用程序,或者我是否将自己编码到角落?

现场版再次在

https://repl.it/repls/CarefulGiganticExtraction

4

0 回答 0