Loading ...
Sorry, an error occurred while loading the content.

for [k,v] in items(dict): Bug? Design?

Expand Messages
  • Maxim Kim
    Hi, the following vimscript gives me an error on plain vim72 with no patches applied: let mdict = { name : vimwiki , data :[]} for [key, value] in
    Message 1 of 3 , Mar 2, 2009
    • 0 Attachment
      Hi,
      the following vimscript gives me an error on plain vim72 with no
      patches applied:

      let mdict = {"name": "vimwiki", "data":[]}
      for [key, value] in items(mdict)
      " echo key value
      endfor

      E706: Variable type mismatch for: value.

      :h E706 gives some explanation -- it looks like there is "change the
      type of a variable" from string to a list.

      So, it looks like for loop cannot iterate over composite data.
      Just tried:

      let lst = ["hello", []]
      for value in lst
      endfor

      Gives me the same error.
      I'd expect it to do :unlet key, :unlet value before each iteration.
      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Maxim Kim
      ... He he. Note on :h for made it clear. ... I have to unlet variables explicitly: let lst = [ hello , []] for value in lst unlet value endfor This is
      Message 2 of 3 , Mar 2, 2009
      • 0 Attachment
        > Just tried:
        >
        > let lst = ["hello", []]
        > for value in lst
        > endfor
        >
        > Gives me the same error.
        > I'd expect it to do :unlet key, :unlet value before each iteration.

        He he.
        Note on :h for made it clear.

        >>Note that the type of each list item should be
        >>identical to avoid errors for the type of {var}
        >>changing. Unlet the variable at the end of the loop
        >>to allow multiple item types.

        I have to unlet variables explicitly:
        let lst = ["hello", []]
        for value in lst
        unlet value
        endfor

        This is inconvenient but usable. :)

        Just curious, why for loop implemented that way?
        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • Marc Weber
        ... I don t like this vim behaviour either. However you can work around it: let lst = [ hello , []] for value in lst do stuff unlet value endfor Marc Weber
        Message 3 of 3 , Mar 2, 2009
        • 0 Attachment
          On Mon, Mar 02, 2009 at 07:21:09AM -0800, Maxim Kim wrote:
          > [...]
          > E706: Variable type mismatch for: value.
          >
          > So, it looks like for loop cannot iterate over composite data.
          > Just tried:
          >
          > let lst = ["hello", []]
          > for value in lst
          > endfor
          >
          > Gives me the same error.
          > I'd expect it to do :unlet key, :unlet value before each iteration.

          I don't like this vim behaviour either. However you can work around it:

          let lst = ["hello", []]
          for value in lst
          " do stuff
          unlet value
          endfor

          Marc Weber

          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        Your message has been successfully submitted and would be delivered to recipients shortly.