问题
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