Unexpected error when parsing what seems to be reasonable code (Bug in parser maybe?)

81 views
Skip to first unread message

Dwayne Crooks

unread,
Apr 22, 2017, 6:33:18 PM4/22/17
to Elm Discuss
Hi,

The following snippet of code:

view : Model -> Html Msg
view model =
    let
        ratingViewConfig =
            { Rating.defaultViewConfig | readOnly = model.readOnly }
    in
        div []
            [ Html.map SetRatingState <|
                Rating.view ratingViewConfig model.ratingState
            , label []
                [ input
                    [ type_ "checkbox"
                    , checked model.readOnly
                    , onClick ToggleReadOnly
                    ]
                    []
                , text "read only"
                ]
            ]

causes the following error to occur:

I ran into something unexpected when parsing your code!
63|             { Rating.defaultViewConfig | readOnly = model.readOnly }
                  ^
I am looking for one of the following things:
    a closing bracket '}'
    a lower case name
    whitespace

However, if I update the let bindings as follows:

    let
        defaultViewConfig = Rating.defaultViewConfig
        ratingViewConfig =
            { defaultViewConfig | readOnly = model.readOnly }
    in
        ...

Then, there is no error. Am I missing something or is this a bug in the parser?

Peter Damoc

unread,
Apr 23, 2017, 3:42:12 AM4/23/17
to Elm Discuss
On Sat, Apr 22, 2017 at 11:50 AM, Dwayne Crooks <dwayne...@gmail.com> wrote:
However, if I update the let bindings as follows:

    let
        defaultViewConfig = Rating.defaultViewConfig
        ratingViewConfig =
            { defaultViewConfig | readOnly = model.readOnly }
    in
        ...

Then, there is no error. Am I missing something or is this a bug in the parser?

This is related to an active topic around record update syntax. 
In short, currently it is not allowed to directly update nested records. ({ Rating.defaultViewConfig | readOnly = model.readOnly } is invalid syntax) 
The temporary solution is the one you stumbled upon: creating an alias for the nested record. 

Another solution would be to use custom functions:


updateReadOnly : { a | readOnly : Bool } -> Bool -> a
updateReadOnly rec readOnly =
    { rec | readOnly = readOnly }

and then use it like: 

ratingViewConfig =
    updateReadOnly Rating.defaultViewConfig True


--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Ilias Van Peer

unread,
Apr 23, 2017, 2:03:17 PM4/23/17
to Elm Discuss
If I'm not mistaken, this isn't so much about nested records as it is about referencing a qualified record in a record update expression.

This seems related to - but not exactly the same thing as - https://github.com/elm-lang/elm-compiler/issues/1549

So yeah, you could either create a local binding for it, use an extra helper function like Peter described, or expose `defaultViewConfig` in your imports so you can use it unqualified.

I've created a new issue for this - https://github.com/elm-lang/elm-compiler/issues/1587

Op zondag 23 april 2017 09:42:12 UTC+2 schreef Peter Damoc:

Dwayne Crooks

unread,
Apr 25, 2017, 8:33:01 AM4/25/17
to Elm Discuss
Thanks guys! Both responses were helpful.
Reply all
Reply to author
Forward
0 new messages