Iteration protocol: a sentinel value?

# Axel Rauschmayer (12 years ago)

In the TC39 meeting notes, Mark suggested something similar (but more sophisticated) which was rejected and I am wondering why.

Herman’s protocol is (roughly):

  • Values v: { value: v }
  • After last value: { done: true }

With a sentinel value, this would look like:

  • Values v: v
  • After last value: SENTINEL_VALUE (defined once, somewhere)

The latter seems simpler to me – what’s wrong with it?

Thanks!

Axel

# André Bargull (12 years ago)

The sentinel cannot carry a return value, from the notes:

# Axel Rauschmayer (12 years ago)

Thanks! Can you elaborate?

# André Bargull (12 years ago)

return <expression> is allowed within a generator [1] and to be able

to retrieve the <expression>'s value, a (possibly frozen) sentinel value

doesn't quite work.

A simple example:

function* gen() { return 123; }

var {value, done} = gen().next(); assertEq(done, true); assertEq(value, 123);

[1] harmony:generators#returning

# Axel Rauschmayer (12 years ago)

I’m guessing that the semantics are not negotiable, but if they were, I would let an empty return terminate the generator, while

return expr;

would be synonymous to

yield expr;
return;

That would more clearly separate concerns – I’m not too fond of the stopping mechanism (be it {done} or StopIteration) having to play double duty.

# Brendan Eich (12 years ago)