compiler
compiler copied to clipboard
Unable to make a Union type with Function
Quick Summary: Unable to make a Union type with Function
SSCCE
-- This does not compile
type Updater
= (Model -> Model)
type Msg
= Updater
| Increment
update msg model
= case msg of
Updater -> msg model
Increment -> {model | count = model.count+1}
- Elm: 1.0.1
- Browser: Chrome
- Operating System: OSX Mojave
Additional Details
This would be very useful for defining simple updates to the model inline without adding new messages to the Msg type.
div [onClick \model -> {model | model.count - 1}] [text "-"]
Thought by changing type Updater = Model -> Model to type alias Updater = Model -> Model would solve the problem but it seems like the compiler doesn't like matching a function to the Msg union type.
module Main exposing (main, pluralize)
import Basics exposing (abs)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick)
type alias Updater =
Model -> Model
type Msg
= Updater
| OtherMessages
type alias Model =
{ count : Int }
init =
{ count = 1 }
main =
Browser.sandbox
{ init = init
, update = update
, view = view
}
updateCount : (Int -> Int) -> Updater
updateCount f =
\model -> { model | count = f model.count }
view model =
div []
[ button [ onClick (updateCount (\i -> i + 1)) ] [ text "+" ]
, div [] [ text (pluralize "ox" "oxen" model.count) ]
, button [ onClick (updateCount (\i -> i - 1)) ] [ text "-" ]
]
update : Msg -> Model -> Model
update msg model =
case msg of
Updater ->
msg model
pluralize : String -> String -> Int -> String
pluralize singular plural quantity =
String.fromInt quantity
++ " "
++ (if abs quantity == 1 then
singular
else
plural
)
module Main exposing (main, pluralize)
import Basics exposing (abs)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick)
type alias Updater =
Model -> Model
type Msg
= Updater
| OtherMessages
type alias Model =
{ count : Int }
init =
{ count = 1 }
main =
Browser.sandbox
{ init = init
, update = update
, view = view
}
updateCount : (Int -> Int) -> Updater
updateCount f =
\model -> { model | count = f model.count }
view model =
div []
[ button [ onClick (updateCount (\i -> i + 1)) ] [ text "+" ]
, div [] [ text (pluralize "ox" "oxen" model.count) ]
, button [ onClick (updateCount (\i -> i - 1)) ] [ text "-" ]
]
update : Msg -> Model -> Model
update msg model =
case msg of
Updater ->
msg model
pluralize : String -> String -> Int -> String
pluralize singular plural quantity =
String.fromInt quantity
++ " "
++ (if abs quantity == 1 then
singular
else
plural
)
yields the following error:
-- TYPE MISMATCH -------------------------------------------------- src/Main.elm
The 1st argument to `sandbox` is not what I expect:
26| Browser.sandbox
27|> { init = init
28|> , update = update
29|> , view = view
30|> }
This argument is a record of type:
{ init : { count : Basics.Int }
, update : Msg -> { count : Basics.Int } -> { count : Basics.Int }
, view : { count : Basics.Int } -> Html (Model -> Model)
}
But `sandbox` needs the 1st argument to be:
{ init : { count : Basics.Int }
, update : Msg -> { count : Basics.Int } -> { count : Basics.Int }
, view : { count : Basics.Int } -> Html Msg
}
-- TOO MANY ARGS -------------------------------------------------- src/Main.elm
The `msg` value is not a function, but it was given 1 argument.
50| msg model
^^^
Are there any missing commas? Or missing parentheses?
Try this:
type Msg =
| Updater (Model -> Model)
| OtherMessages
-- in view code
div [ onClick (Updater (\model -> { model | count = model.count + 1 }) ] [ ... ]
I think the #general channel in the elm slack might be a good place to discuss this issue further.
Thanks for the quick reply! I'll direct my further questions there.