compiler icon indicating copy to clipboard operation
compiler copied to clipboard

Unable to make a Union type with Function

Open landrito opened this issue 7 years ago • 3 comments

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 "-"]

landrito avatar May 19 '19 08:05 landrito

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?

landrito avatar May 19 '19 08:05 landrito

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.

matheus23 avatar May 19 '19 09:05 matheus23

Thanks for the quick reply! I'll direct my further questions there.

landrito avatar May 19 '19 14:05 landrito