tc39 / proposal-do-expressions

Proposal for `do` expressions
MIT License
1.11k stars 14 forks source link

Can do expression be lazy evaluation? #42

Closed zheeeng closed 3 years ago

zheeeng commented 5 years ago

For better performance consideration. Expect

let x = do {
  let tmp = f();
  console.log(42)
  tmp * tmp + 1
};

if (true) {
    x // print 42 in console
}
jhpratt commented 5 years ago

Just wrap it in a function? Not every case needs to be lazy.

moritzuehling commented 4 years ago

I feel like this would be more of a "gotcha!" than a feature. Also, consider this case:

let a = 2;

const x = do {
  let tmp = 4;
  tmp + a
}

a = 4;

if (true) {
  console.log('x is', x);  // does this print 6 or 8?
}

I feel like this would make the behavior of this function way less obvious. Lazily evaluated variables might be a nice-to-have, but I don't think this is the right place.

bakkot commented 3 years ago

That would be a pretty different thing than what is proposed here; I think it would need to be pursued as a different proposal. (Separately, I personally would not be in favor of it.) I'm going to close this issue as I don't think we ought to consider it an open question for this proposal.