dotnet / vblang

The home for design of the Visual Basic .NET programming language and runtime library.
288 stars 64 forks source link

The Iterator should have its own block! #400

Open VBAndCs opened 5 years ago

VBAndCs commented 5 years ago

This may not be an urgent proposal, but it is a logical one and a syntactic suger. Iterator should be an independent block not a function. One obvious reason, it they don't work in the same way, nor use the same return instruction! For example, this iterator will not compile because the return keyword is not allowed, although it seems as perfect function that should work:

Public Iterator Function Foo(n As IEnumerable) As IEnumerable(Of String)
    Return n
End Function

So, I ask to define Integrators like this:

Public Iterator Foo(n As IEnumerable) As IEnumerable(Of String)
    For Each i In n
        Yield "Element" + i
    Next 
End Iterator

This is clearer than Iterator Function definition , and is better in lambdas because the elimination of the long word Function:

Dim t = Iterator (n As IEnumerable)
    For Each i In n
        Yield "Element" + i
    Next 
End Iterator

So now, we have Sub, Function, and Iterator blocks.

This change will not break any code, because the old Iterator Function syntax will still working, but VB editor should change it to the 'Iterator' syntax once the code file is opened. Any new Iterator Function will be changed instantly to Iterator.

Note: There is no such issue in C#, since there is no Function, end function keywords. So, there was no real duality to keep between the two languages when the Iterator keyword was added.

VBAndCs commented 5 years ago

Any response?

pricerc commented 5 years ago

Perhaps because very few people actually write Iterators?

I think I can count on two fingers the number of iterators I've written in the last 20 years. But I've done a lot of inheriting from base classes that have already implemented them for me, so I'm not doing it myself.

That said, having thought about it for very few minutes, that I think that the semantics are different enough that it's not unreasonable that Iterators be considered a new kind of method (discrete from procedures/subs and functions), and that they should Yield their values, where Functions should Return their value. Note that an iterator by definition yields multiple values, and a function returns exactly one (which may be a collection or IEnumerable, but that's another discussion, I think).

VBAndCs commented 5 years ago

@pricerc I wrote this as a general reply #404 :) Thanks.