Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • S sweet-core
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 62
    • Issues 62
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 4
    • Merge requests 4
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • sweet-js
  • sweet-core
  • Issues
  • #384
Closed
Open
Issue created Sep 14, 2014 by Administrator@rootContributor

Optional syntax

Created by: retorquere

I have a macro

macro iterate {

  // iterate (value: index from items)

  rule { ($($key:ident : $value:ident from $dict:expr)) { $body... } }   => { 
    let dict = $dict;
    let keys = Object.keys(dict);
    let length = keys.length;
    let index = 0;
    while (index < length) {
      let $key = keys[index];
      if (!dict.hasOwnProperty($key)) { continue; }
      let $value = dict[$key];
      $body...
      index++;
    }
    dict = undefined;
    keys = undefined;
  }
}

which allows for

iterate (key:value from {a: 1, b: 2}) {
  ...
}

If I use this twice in a row however, jshint complains that I'm re-declaring the variable, and Firefox (this is code for an extension, so it's chrome-javascript, not web-javascript) complains TypeError: redeclaration of variable key. Is there a way to rewrite this so I could do

iterate (var key: var value from {a: 1, b: 2})
iterate (key: let value from {a: 1, b: 2})

and follow the hints provided by the var/let to declare (or not) key and value using these?

Assignee
Assign to
Time tracking