(Bug) For..Step negative values not working.

About Monkey 2 Forums Monkey 2 Development (Bug) For..Step negative values not working.

This topic contains 8 replies, has 6 voices, and was last updated by  nobuyuki 2 years, 10 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #886

    impixi
    Participant

    For..Step negative values not working. Example:

    #899

    Amon
    Participant

    Function Main is reserved I think? No?

    Tested this though and it works fine for me but I didn’t have the code within a Function Main().

    #900

    therevills
    Participant

    Function Main is the starting point for all MonkeyX’s (1 & 2) application. You can put anything in there you like (as long as its valid MX2).

    When I tested the negative step on v0.008 it doesnt do anything…

    #901

    impixi
    Participant

    That snippet should print out the numbers 5 to 1.

    I’m actually shocked this bug has remained hidden this far into development (or maybe it got introduced recently?). Reverse cycling through a loop is a fundamental programming task – or at least it used to be. 😉

    I only found it because I’ve ported some old BlitzMax code that showed bizarre results in MX2 for no obvious reason. Stumped me for *hours*… Kinda relieved the problem isn’t in my code after all. 🙂

    Edit:
    @amon
    Where did you have the code? Reverse For loop iteration fails for me in class Methods too.

    #902

    therevills
    Participant

    Still fails in v0.009:

    Which outputs the following:

     

     

    #908

    Amon
    Participant

    Hmm my bad.  It works but the output is not what it should be. I didn’t check that. Apologies.

    After reading both replies I went back and did testing I should have done when first reading the OP.  Again, though it works but the output is all wrong much like therevills has shown.

     

     

     

     

    #913

    Danilo
    Participant

    What about adding DownTo (additional to ‘Step -1’)?

    [/crayon] [/crayon]
    #929

    Mark Sibly
    Keymaster

    Negative step should be working now.

    I think I will re-add the ‘c’ style ‘for’ that used to be in there though as it’s the most flexible.

     

    #1024

    nobuyuki
    Participant

    Please consider adding c-style loops carefully.  Some people (especially c coders) will not take to using basic style loops at all, even if it is the recommended standard convention, and this is one of the cornerstones of what many would consider the readability of basic-style syntax.  (I consider Monkey syntax even more readable then standard Basic when the step value is positive if using Until!)

    As long as the step value can be arbitrary, it retains most of the flexibility of a c-style for loop. With a custom iterator, it should be able to do just about everything.

     

    Presented for your consideration:  https://msdn.microsoft.com/en-us/library/dscyy5s0.aspx

    Due to the way iterators were implemented in the different languages (possibly related to foreach syntax), their articles are different, and VB didn’t get custom iterators until 7 years later.

     

    Though I would recommend against reserving a keyword for something as simple and arbitrary as a negative step value, perhaps this might be okay if specifying a custom iterator.  (IteratingBy x? Step x if x is a Func? Idk..)  in that case, the iterator method would have a standard pattern whereby all for loops invoking one would have the loop bounds available to it as arguments.. maybe a bit clunky, like old Property syntax…

    Ideally I’d like to specify an arbitrary step value (positive, negative, and integer runtime variables that aren’t consts) without c-style syntax, but if it’s not possible for whatever reason (and if that’s why you’re leaning towards enabling c-style for loops), please consider this.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.