LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Questionable semantics: list comprehension in `yield` expression

Open summivox opened this issue 10 years ago • 1 comments

Closest issue I can find is #667


->* yield [x for x in arr]

compiles to:

(function*(){
  var x;
  return (yield (yield* (function*(){
    var i$, ref$, len$, results$ = [];
    for (i$ = 0, len$ = (ref$ = arr).length; i$ < len$; ++i$) {
      x = ref$[i$];
      results$.push(x);
    }
    return results$;
  }())));
});

->* yield alert [x for x in arr]

compiles to:

(function*(){
  var x;
  return (yield alert((yield* (function*(){
    var i$, ref$, len$, results$ = [];
    for (i$ = 0, len$ = (ref$ = arr).length; i$ < len$; ++i$) {
      x = ref$[i$];
      results$.push(x);
    }
    return results$;
  }()))));
});

While in all cases the behavior is as expected, the compilation is surprising and undocumented. Clearly the yield* is extraneous in both cases. I suspect there must have been use cases where the yield* makes sense, but could we at least document this, and clarify when this rule is applicable?

summivox avatar Aug 08 '15 15:08 summivox

This bug appears with switch statements as well:

(name) ->*
  x = switch name
  | 'a' => 0
  | 'b' => 1
  return x

compiles to

// Generated by LiveScript 1.5.0
(function(){
  (function*(name){
    var x;
    x = (yield* (function*(){
      switch (name) {
      case 'a':
        return 0;
      case 'b':
        return 1;
      }
    }()));
    return x;
  });
}).call(this);

gkovacs avatar Mar 30 '17 09:03 gkovacs