Skip to content

Instantly share code, notes, and snippets.

@owen-d
Last active July 11, 2017 18:08
Show Gist options
  • Save owen-d/b727b9a1a5d4990ed947c6fa7056a92a to your computer and use it in GitHub Desktop.
Save owen-d/b727b9a1a5d4990ed947c6fa7056a92a to your computer and use it in GitHub Desktop.
issue subtyping elm msgs
module Lib.SubMsg exposing (..)
type SubMsg
= SubMsgA
| SubMsgB
module Lib.SubUpdate exposing (..)
import Msg exposing (Msg(SubMsg))
import Lib.SubMsg exposing (..)
subUpdate : SubMsg -> Model -> (Model, Cmd Msg)
subUpdate msg model =
case msg of
SubMsgA ->
...
SubMsgB ->
...
module Msg exposing (..)
import Lib.SubMsg exposing (SubMsg)
type Msg
...
| SubMsg Submsg
module Update exposing (..)
import Msg exposing (Msg(..))
import Lib.SubUpdate exposing (subUpdate)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
...
SubMsg msg_ ->
subUpdate msg_ model
module View exposing (..)
import Msg exposing (Msg(SubMsg))
import Lib.SubMsg exposing (SubMsg(SubMsgA, SubMsgB))
import Html exposing (..)
view: Model -> Html Msg
view model =
Html.div [ onClick SubMsgA ] [...]
{- Errors:
expecting:
List (Html Msg)
but it is:
List (Html SubMsg)
-}
{-
Note: it works if you wrap it with a function like `cast` below.
Is there a better way/shorthand to do this?
-}
cast : (a -> SubMsg) -> a -> Msg
cast producer msg =
producer msg
|> SubMsg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment