New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope in let and a few bugs #667

Closed
jschoch opened this Issue Jul 17, 2016 · 5 comments

Comments

Projects
None yet
3 participants
@jschoch

jschoch commented Jul 17, 2016

i was trying to do a nested update and found an odd bug i recreated here. Start by clicking "not Borked". this seems to correctly change the value to 'model.r' to "XXX".

Then click "bork 2", which changes 'model.r' to

Finally Clicking Bork creates a Uncaught TypeError: Cannot read property 'test' of undefined error.

The docs are not very clear on scoping of let. i've seen other places where the mutation is not as I would predict. As a newb in elm I have no idea how the scope works, I would expect I shouldn't be able to mutate stuff in place.

this was tested via the try elm function of your website.

import Html exposing (div, button, text, hr, br)
import Html.App exposing (beginnerProgram)
import Html.Events exposing (onClick)

type alias Model =
  { count : Int
  , dbg : List String
  , val : String
  , r : XRec
  , test : Bool
  , m : M
  }

type alias XRec = { x : String}

mdl : Model
mdl = {
      count = 0
      , dbg = [""]
      , val = "shoot"
      , r = { x = "x"}
      , test = True
      , m = m}

main =
  beginnerProgram { 
    model = mdl
    , view = view
    , update = update }


view model =
  div []
    [ button [ onClick Decrement ] [ text "-" ]
    , div [] [ text (toString model) ]
    , button [ onClick Increment ] [ text "+" ]
    , hr [] []
    , text (toString m)
    , button [ onClick Bork ] [ text "bork"]
    , br [] []
    , button [ onClick Bork2 ] [ text "bork 2"]
    , br [] []
    , button [ onClick NotBorked] [ text "not Borked"]
    ]


type Msg = Increment | Decrement | Bork | Bork2 | NotBorked


update msg model =
  case msg of
    Increment ->
      {model | count = model.count + 1, dbg = "inc" :: model.dbg}

    Decrement ->
      {model | count = model.count - 1, dbg = "dec" :: model.dbg}

    Bork ->
      let
        model = if model.test == True then
          let 
            a = { model | val = " sweet "}
            oldR = a.r
            newR = {oldR | x = "XXX"}
            model = { a | r = newR}
          in model
        else
          { model | dbg = "bank" :: model.dbg}

      in
        --model
      {model  | dbg = "bork" :: model.dbg}

    Bork2 ->
      let 
        model = doBork model
      in
        model

    NotBorked ->
      let
        model = donotBork model
      in 
        model

doBork : Model -> Model
doBork model = 
  let
    a = { model | val = "bam"}
    oldR = a.r
    newR = { oldR | x = "XXX"}
    model = { a  | r = newR}
  in 
    model

donotBork : Model -> Model
donotBork model = 
  let
    a = { model | val = "bam"}
    oldR = a.r
    newR = { oldR | x = "XXX"}
    mdl = { a  | r = newR}
  in 
    mdl


type alias M = {
  s : String
  ,l : List String
}

m : M
m = { s = "String", l = ["list","of","strings"]}


@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Jul 17, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Jul 17, 2016

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jul 17, 2016

Contributor

Please check issue 873 in the elm-compiler repo.

Contributor

jvoigtlaender commented Jul 17, 2016

Please check issue 873 in the elm-compiler repo.

@jschoch

This comment has been minimized.

Show comment
Hide comment
@jschoch

jschoch Jul 17, 2016

for the doBork function how is this recursive? isn't this a variable shadowing issue?

jschoch commented Jul 17, 2016

for the doBork function how is this recursive? isn't this a variable shadowing issue?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jul 17, 2016

Contributor

In this let-block:

  let
    a = { model | val = "bam"}
    oldR = a.r
    newR = { oldR | x = "XXX"}
    model = { a  | r = newR}

the binding for a depends on model and the binding for model depends on a. In particular, the model you use in the binding for a is not the model from the "surrounding"

doBork model = 
  ...

Instead, it is the model from the binding inside the let-block, no matter that it appears a few lines below.

This is just a fact of how let-blocks work in Elm (and in Haskell, but not in OCaml, if you need comparisons to other languages).

Contributor

jvoigtlaender commented Jul 17, 2016

In this let-block:

  let
    a = { model | val = "bam"}
    oldR = a.r
    newR = { oldR | x = "XXX"}
    model = { a  | r = newR}

the binding for a depends on model and the binding for model depends on a. In particular, the model you use in the binding for a is not the model from the "surrounding"

doBork model = 
  ...

Instead, it is the model from the binding inside the let-block, no matter that it appears a few lines below.

This is just a fact of how let-blocks work in Elm (and in Haskell, but not in OCaml, if you need comparisons to other languages).

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jul 17, 2016

Contributor

If you need more explanation, please consult one of the community venues, for example the mailing list. Several options are listed at http://elm-lang.org/community.

This issue tracker here is really for bugs and things that need to change in core.

Contributor

jvoigtlaender commented Jul 17, 2016

If you need more explanation, please consult one of the community venues, for example the mailing list. Several options are listed at http://elm-lang.org/community.

This issue tracker here is really for bugs and things that need to change in core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment