Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Is there a tool out there for unraveling iterators and yield from C# code?

Is there a tool out there for unraveling iterators and yield from C# code?

Scheduled Pinned Locked Moved The Lounge
csharpdesignasp-netcomgraphics
17 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H honey the codewitch

    Iterators can have try catch, but they cannot "break" try catches, so you can't have a yield in a try. Consider how iterators work. Basically they turn your loops into gotos and build a state machine around your code in order to "continue" it, but to continue a try block would require additional catch statements for each continuation, for starters (and I haven't thought it through all the way so it may not even be doable). It might be doable, but it wouldn't be pretty.

    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #8

    honey the codewitch wrote:

    it wouldn't be pretty.

    I don't care how ugly it might be in the background. That's the beauty of high level languages.

    H 1 Reply Last reply
    0
    • P PIEBALDconsult

      honey the codewitch wrote:

      it wouldn't be pretty.

      I don't care how ugly it might be in the background. That's the beauty of high level languages.

      H Offline
      H Offline
      honey the codewitch
      wrote on last edited by
      #9

      I should have been more clear. It will probably add significant bloat and maybe even be a performance hit - not something you'd necessarily want to have happen behind your back - if it's even possible to do as I said. One problem is if your code yields in a catch block as well. You can't goto into or out of one, IIRC so I don't know how you'd break that up. It may not be possible after all, and even if it was you'd probably have to duplicate the code in your catch blocks N times where N is the number of yields or something (not enough coffee to work it all out yet)

      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

      1 Reply Last reply
      0
      • H honey the codewitch

        He probably said that before "async/await" :)

        Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #10

        He did indeed. It was like fifteen years ago or so. You also couldn't combine yield and async in the beginning.

        Wrong is evil and must be defeated. - Jeff Ello

        1 Reply Last reply
        0
        • H honey the codewitch

          This is going to sound funny, but I'm trying to update some old code to use my new regex DFA format and corresponding lexing code, so I have reasons. My new code uses C# iterators rather than manually implementing IEnumerator The problem is my lexer generator generates code in a language independent manner the same way ASP.NET does when it compiles pages - it uses the CodeDOM. The CodeDOM does not support iterators, so I have to implement the IEnumerator interface w/ corresponding state machine myself. I can do it, but I was hoping for something where I didn't have to. :) Now, it's possible that I could run code compiled with iterators in it through a decompiler tool like Reflector, but the result is nasty and really hard to follow. And this is kind of hard to google. I was just curious if anyone had ever seen such a tool, or otherwise has any ideas.

          Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #11

          honey the codewitch wrote:

          my lexer generator generates code in a language

          I presume you really do mean that it is yours. That is the problem of course with doing your own code generation. Of course your real choices are to update that tool or stop using iterators. Myself I would probably just not use iterators. There probably isn't that much impacted code.

          H 1 Reply Last reply
          0
          • J jschell

            honey the codewitch wrote:

            my lexer generator generates code in a language

            I presume you really do mean that it is yours. That is the problem of course with doing your own code generation. Of course your real choices are to update that tool or stop using iterators. Myself I would probably just not use iterators. There probably isn't that much impacted code.

            H Offline
            H Offline
            honey the codewitch
            wrote on last edited by
            #12

            Maybe I wasn't clear. In order to update the tool, I need to port code from stuff that uses iterators to stuff that doesn't.

            Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

            J 1 Reply Last reply
            0
            • H honey the codewitch

              Maybe I'm old but I'm sort of put off by ChatGPT. I don't trust it. But I guess it would work for this because I can immediately verify what it tells me. Thanks.

              Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #13

              To this day, AI has been going where I have already "gone before" in my current interests. Depending on the subject matter (i.e. obscure), it's pretty easy to exhaust what's online. You waste time looking at what you've seen before; except now the AI has gotten "creative" with it.

              "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

              1 Reply Last reply
              0
              • H honey the codewitch

                Maybe I wasn't clear. In order to update the tool, I need to port code from stuff that uses iterators to stuff that doesn't.

                Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #14

                Ok? Not sure I see how that changes what I suggested? Porting code always involves changes. Otherwise it would just be copying code.

                H 1 Reply Last reply
                0
                • J jschell

                  Ok? Not sure I see how that changes what I suggested? Porting code always involves changes. Otherwise it would just be copying code.

                  H Offline
                  H Offline
                  honey the codewitch
                  wrote on last edited by
                  #15

                  It's really difficult to port iterators away to non-iterator code for anything non-trivial. The necessary transformation is pretty complicated, and involves building state machines and using a bunch of gotos.

                  Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                  J 1 Reply Last reply
                  0
                  • H honey the codewitch

                    It's really difficult to port iterators away to non-iterator code for anything non-trivial. The necessary transformation is pretty complicated, and involves building state machines and using a bunch of gotos.

                    Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #16

                    honey the codewitch wrote:

                    away to non-iterator code for anything non-trivial.

                    Do we have a different definition of iterator? What I am thinking of is the following. No idea how you can get to a state machine from that. Iterators - C# | Microsoft Learn[^]

                    H 1 Reply Last reply
                    0
                    • J jschell

                      honey the codewitch wrote:

                      away to non-iterator code for anything non-trivial.

                      Do we have a different definition of iterator? What I am thinking of is the following. No idea how you can get to a state machine from that. Iterators - C# | Microsoft Learn[^]

                      H Offline
                      H Offline
                      honey the codewitch
                      wrote on last edited by
                      #17

                      Yep, that's iterators. Note that there is no intrinsic .NET support for this feature. It is a feature of the C# compiler, which works by transforming the code. The C# compiler transforms it into a state machine based coroutine by A) hoisting local variables to make them members of the IEnumerator implementation. B) Implementing MoveNext() such that wherever there's a yield, that's a new state in the state machine. Because the routine actually returns at that point. When you call it again, it is "restarted" where it left off using the state machine. All iterators are state machines once the compiler is done mangling that code.

                      Check out my IoT graphics library here: https://honeythecodewitch.com/gfx And my IoT UI/User Experience library here: https://honeythecodewitch.com/uix

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups