Hello guys
Hello guys,
New Feature request - Iterators (aka. yield) - https://quality.embarcadero.com/browse/RSP-13341
Yield operator support for Delphi.
Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration). With yield support we are able to create an iterator with only few lines of code.
It could follow the same rules as the .NET version (https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx).
Oxygene language have also an implementation for Yield. (http://docs.elementscompiler.com/Oxygene/Members/Iterators/).
:D
https://quality.embarcadero.com/browse/RSP-13341
New Feature request - Iterators (aka. yield) - https://quality.embarcadero.com/browse/RSP-13341
Yield operator support for Delphi.
Using yield to define an iterator removes the need for an explicit extra class (the class that holds the state for an enumeration). With yield support we are able to create an iterator with only few lines of code.
It could follow the same rules as the .NET version (https://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx).
Oxygene language have also an implementation for Yield. (http://docs.elementscompiler.com/Oxygene/Members/Iterators/).
:D
https://quality.embarcadero.com/browse/RSP-13341
I was not able to find out a cool language syntax for this :(
ReplyDeleteHorácio Filho My 2 cents: Why don't you show me a problem that can be solved with this new feature, which can't be solved easily with the features that Delphi gives you today?
ReplyDeleteI've seen people asking lots of new features because that new "super duper" feature will save him 20 keystrokes per day... I'm not saying that this is your case, but I've seen this quite a lot, lately...
Alexandre Machado This new feature does not give more power to Delphi, adding it to the language or not doesn't change the possibility of achieve the same results, you still able to enumerate over a enumerable collection before or after that, the main point here is simplicity and less lines of code.
ReplyDeleteWhen you need to iterate (or enumerate) over a enumerable type you have to create a class to implement the enumerator behavior, you have to deal with unexpected situations like unstarted enumeration, and finished enumeration, this is boilerplate code and can be avoided. Inside the class is the best place to specify the enumerator behavior.
I'll give you one example:
ReplyDeletehttps://quality.embarcadero.com/browse/RSP-13386
Allow us to create objects implementing interfaces without the TInterfacedObject crap, just like it should be since day one!
You should just be able to query ANY object if it is implementor of some interface, and then call the interface methods! Simple!
Also, get rid of the getter/setter necessity for Interface properties.
Alexandre Machado I vote for ARC everywhere, it solves your problem pretty well as long you don't need a derivated TObject class to implement ARC support.
ReplyDeleteFortunately your vote alone won't count much. Lucky us, remaining Delphi developers.
ReplyDelete> This new feature does not give more power to Delphi
ReplyDeleteSo why don't we rather focus on features that give new power to Delphi. Syntax sugar is nice, but adding to Object Pascal all of the syntax sugar of all other languages combined, won't make it a better language... just a very confusing one.
Marco Cantù so, please, take a look at my feature request about interfaces. This is not syntax sugar to look cool with your .NET friends....
ReplyDeleteI just did! Extending interfaces in a way they are COM-free but can still be used for COM (which is still a great feature of the product) is not exactly trivial, but there is significant room for improvement around interfaces, in many different areas.
ReplyDeleteMarco Cantù lets say COM-Free interfaces are added, doesn't that mean, we can define an Interface which resembles a C++ InterfaceClass? (given calling conventions, and parameters are compatible in some way)
ReplyDeleteIf I understand it correctly, the yield statement in c# or Python mostly for generators, I think implementing iterator is just a 'by product' of the yield statement. The advantages of generators? Check this article: https://davidwalsh.name/es6-generators IMHO it's much more than just a syntax sugar Marco Cantù
ReplyDeleteMarco Cantù Syntax sugar IS giving more power to a programming language. Otherwise we could just all code in assembler or IL. I don't get why it always gets down to "can't I do this thing already?" argument. When Delphi release I already could write Windows GUI applications! But why was Delphi a huge success? Because I now could do it easily using components and controls that wrapped all the low Windows API stuff.
ReplyDeleteSame goes for syntax sugar - it enables a developer to write code in a more expressive way. Look at the C# community and their reaction to C# 6 - it was almost 100% syntax sugar and nothing that could not be done differently before - but people love it! And that is not because C# devs are all just hipsters.
Alexandre Machado Syntax sugar is not about saving keystrokes. It is about reducing ceremony to a minimum. Asbjørn Heid made a good example in the comments to my post over here: https://plus.google.com/+StefanGlienke/posts/NYdw5jtyFe8
> Syntax sugar is giving more power to a programming language.
ReplyDeleteI agree, I didn't say the opposite. Just arguing there are missing features in the language that I feel more important, and that we cannot adopt the syntax of all other languages combined (see all other threads Horacio started and his long list of syntax extensions).
And in any case I had already accepted the feature request.
Stefan Glienke I didn't say that syntax sugar is about saving keystrokes. However, the argument "this new feature reduces the size of the code and bla bla bla" is often used.
ReplyDeleteThe problem is useless syntax sugar versus real important stuff, in a team with VERY constrained resources. Why don't people vote for the colon operator, just like Oxygene has implemented? That's some syntax sugar that I'd like to see in Delphi. On the other hand, this specific case is just a waste of resources.
Instead of a "step" option, I would much rater see significantly more efficient for..in enumeration, so that I could write my own Range()-like iterator like in Python and similar goodness without feeling dirty for wasting all those CPU cycles.
ReplyDeleteWoops, wrong post :D Yes to this, no to the other one :)
ReplyDeleteThis is more than syntactic sugar. With yield you can avoid having to build a complete list for an enumerator.
ReplyDeleteMarco Cantù hehehehehehe, Oh no, I shouldn't have said that. I could say this feature is the force awakens :D
ReplyDeleteBob Devine Yeah :D It is a key point about it, yield saves a lot of code, inside the class is the best place to put the iterator behavior.
ReplyDeleteWith yield statement you can generate the enumerable states according to your needs, you don't need to have a previous allocated array or a collection.
ReplyDeleteAsbjørn Heid A global function Range could get implemented using yield :D In the same way as does .NET :D Yield is a great addition to the language. It is true, Allen Bauer, I feel good just with yield support :D I have forgotten for loop step statement.
ReplyDeleteHere is a Delphi yield example, sculptured after the C# implementation.
ReplyDeletehttp://www.gerixsoft.com/blog/delphi/yield
Asbjørn Heid You can write an efficient iterator, just use records. The for in syntax is implemented using a preprocessor so it does not matter if you use interfaces or records. It works the same to the compiler, but the record performance is much better.
ReplyDeleteJohan Bontes For some value of efficient... On my machine a Range enumerable takes 7x as long as a simple for loop.
ReplyDeleteOf course if you do a lot of work in the loop, then that will likely overshadow the looping overhead, but for one the compiler doesn't inline the MoveNext call, even when explicitly declared as inline.
Leif Uneus It is cool :D A problem about this implementation is the lack of cross-platform support, it uses Windows API to achieve the yield behavior and some assembly magic. The major gain of yield is that all its stuff is generated by the compiler, reducing runtime overhead.
ReplyDeleteLeif Uneus That code is actually based on my code from some years ago. Too bad he simplified it way too much so it will not work with nested yields.
ReplyDelete