Removing the 'dispatch' function from the view function
See original GitHub issueHaving now spent some time playing with Fabulous (and really enjoying using it!) I became curious as to why the update function requires the dispatch
function to be passed in which is a little different to Elm. Apologies if there is a well know design reason for it and please let me know if there is!
Fabulous
model : 'model
update : 'msg -> 'model -> 'model
view : 'model -> ('msg -> unit) -> ViewElement
Elm
model : Model
update : Msg -> Model -> Model
view : Model -> Html Msg
When starting with Fabulous it seemed a little odd to have this parameter and it seems Jamie was confused. From looking through the code I think its there to ensure type safety for the commands on the ViewElements? This lead me to think could ViewElement
be defined as ViewElement<'msg>
in something like the following pseudo code:
type ViewElement<'msg> =
{ text : string
command : 'msg option
children : ViewElement<'msg> list option }
member x.Create() : obj = new obj()
member x.UpdateIncremental(prev: ViewElement<'msg>, target: obj) : unit = ()
type View() =
static member inline Button<'msg> (text: string, command : 'msg) = { text = text; command = Some command; children = None }
static member inline Label (text: string) = { text = text; command = None; children = None }
static member inline ContentPage (children : ViewElement<'msg> list) = { text = ""; command = None; children = Some children }
type Model = { Count : int }
type Msg = Increment
let init () = { Count = 0 }
let update (msg : Msg) (model : Model) : Model =
match msg with
| Increment -> { model with Count = model.Count + 1 }
let view (model : Model) =
View.ContentPage(
children =
[ View.Label(text = string model.Count)
View.Button(text = "Increment", command = Increment)])
let program = mkSimple init update view
This constrains the command to all be of type Msg
and would give the classic elm architecture of:
model : 'model
update : 'msg -> 'model -> 'model
view : 'model -> ViewElement<'msg>
Not sure if this is possible or desirable but I thought I would ask the question?
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (4 by maintainers)
Top GitHub Comments
Just adding 2c: if I had control over view DSL (as Fabulous does), I’d have avoided passing
dispatch
explicitly. This has nothing to do with Elmish (which still needs to pass the dispatch into whatever renderes the view) and everything to do with the DSL that was available to implement Elmish.React. If you look at the issue it is still on my mind.v2 removes dispatch from the view function 😃