Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've been working on an ECMAScript dialect for quite some time now and have reached a point where I am comfortable adding new language features. I would love to hear some thoughts and suggestions on the syntax.

Example

generator {
    yield 1;
    yield 2;
    yield 3;
    if (true) {
        yield break;
    }
    yield continue generator {
        yield 4;
        yield 5;
        yield 6;
    };
}

Syntax

GeneratorExpression:
    generator  {  GeneratorBody  }

GeneratorBody:
    GeneratorStatementsopt

GeneratorStatements:
    StatementListopt GeneratorStatement GeneratorStatementsopt

GeneratorStatement:
    YieldStatement
    YieldBreakStatement
    YieldContinueStatement

YieldStatement:
    yield  Expression  ;

YieldBreakStatement:
    yield  break  ;

YieldContinueStatement:
    yield  continue  Expression  ;

Semantics

The YieldBreakStatement allows you to end iteration early. This helps avoid deeply indented code. You'll be able to write something like this:

generator {
    yield something1();
    if (condition1 && condition2) 
        yield break;
    yield something2();
    if (condition3 && condition4) 
        yield break;
    yield something3();
}

instead of:

generator {
    yield something1();
    if (!condition1 && !condition2) {
        yield something2();
        if (!condition3 && !condition4) {
            yield something3();
        }
    }
}

The YieldContinueStatement allows you to combine generators:

function generateNumbers(start) {
    return generator {
        yield 1 + start;
        yield 2 + start;
        yield 3 + start;
        if (start < 100) {
            yield continue generateNumbers(start + 1);
        }
    };
}
share|improve this question
1  
That's not an iterator, it's a generator or coroutine (not the same thing, but it's hard to tell which one it is). Also, the syntax is the least important (but usually - sadly - most discussed) aspect of a language feature. Why not discuss semantics instead (e.g. I'm not quite sure what yield continue does)? – delnan Jan 30 '11 at 0:07
@delnan - The reason I made the keyword iterator is mainly because an iterator expression is transformed into an Iterator object. As to yield continue well due to hubris I thought the semantics were clear but in short it essentially allows you to combine iterators. – ChaosPandion Jan 30 '11 at 0:10
@delnan - Also the semantics are all related to how the iterator body will be transformed into a state machine which may be a bit too much to post as a question. – ChaosPandion Jan 30 '11 at 0:27
5  
@M28 - Thanks for the insightful comment. You may want to consider educating yourself rather than writing off a concept as useless. – ChaosPandion Jan 30 '11 at 0:29
1  
@M28: If you don't know the feature, you won't be able to understand code using it. Yes, right. But that's true for every feature (You do expect people reading your JS code to understand closures, right?). It's not a valid objection to any feature, because if it was, all progress (in programming languages) would come to a halt. If a significant portion failed to "get it" after learning about it, yes, that would be an (even compelling) argument. But that's not the case. They can learn it, and they propably should as it's a useful feature. – delnan Jan 30 '11 at 1:16
show 14 more comments

closed as not constructive by MichaelT, Kilian Foth, Jalayn, World Engineer Apr 17 at 14:20

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

1 Answer

Yes it would be nice to get a better idea of the purpose of the break/continue idea.

Seems there's an argument about utility of iterators. More generally, generators. Lets be clear @M28 is utterly and conclusively wrong. I suggest e goes out and writes all er code as a callback, without reading data from a file. Because yes the fundamental purpose of operating systems is to control invert unusable callbacks (interrupts) delivering data so client code can be master instead of slave, reading the data as required by the program, not when required by external conditions.

The extreme difficulty of writing callbacks is clearly demonstrated in complex application by the abuse of threads to obtain control inversion, when in fact no concurrency is actually desired, simply because the C systems programming language fails to support it.

The technical reason for the intractability of callbacks is the lack of support for maintenance of state. Modern processors provide a stack which integrates control flow (program pointer) and local data to support subroutine calling: a master/slave relation. In fact the lack of control exchange in the model is also it's primary limitation. But starting off with a slave is even worse than this misdesign!

share|improve this answer
You're right let me try and give the basic semantics. – ChaosPandion Jan 30 '11 at 2:27
Do you think using iterator as a keyword will cause more confusion than its worth? – ChaosPandion Jan 30 '11 at 2:49
@ChaosPandion - I wouldn't worry about the confusion. The bigger worry to me would be the number of existing pieces of code that have functions or variables named iterator. – Jason Baker Jan 30 '11 at 3:13
@Jason - I'm considering making the keyword contextual which will help. – ChaosPandion Jan 30 '11 at 3:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.