Missed major changes from v77f to v81b need help.

Monkey Forums/Monkey Programming/Missed major changes from v77f to v81b need help.

benmc(Posted 2014) [#1]
I was monitoring updates religiously for a couple years there, but business changed here, and my last working version of Monkey was v77f.

I updated to the new v81b and all of a sudden, most of my programs are not building.

I would usually do (for example)

For i=0 to 10
 ' something here...
Next i


This made it easy for me to nest things. But now, I get an error and have to delete the "i" after the word Next.

Also, I keep getting errors in my programs in places where I define "Local" variables for some reason. It says I can't anymore.

I'm trying to figure out what happened along the way with these updates. Is there a way I can keep using Next i or Local?


ImmutableOctet(SKNG)(Posted 2014) [#2]
I don't think "Next i" was ever a thing, it'll just work if you use 'Next'. And obviously, you'll have to define 'i' as an actual variable. Well, that's assuming non-strict functionality doesn't already do this for you.

For example:
' Or, you could make 'i' a local variable before this.
' Also, I'd recommend changing this to 'I' instead of 'I', but that's just me.
For Local i:= 0 To 10
	' ...
Next


As for the problems with local variables, are you using 'Local' instead of 'Field'? I really don't have anything to go off of from what you said. Care to give me an example? The fact is, if it's causing errors now, you probably had problems from the beginning.

Hang on, by nest things, did you mean keep the variable around? Because removing the 'Local' in my example, then defining it beforehand will do this.


benmc(Posted 2014) [#3]
I appreciate your feedback, and you were right, my core problem was messy code, coupled with v77f being much more forgiving.

Here is something similar to what I actually had in my code that was working in v77f


Field counter:Int = 0

Method Whatever()

   Local i:Int = 0

   For i = 0 to 10
      counter = counter + 1
   End If

   Local otherVar:Int = 0

End Method



In v77f, this wasn't breaking. But obviously it should have, and in v81b, it threw the error on Local, so I thought I couldn't use Local anymore, but I didn't even notice the End if instead of a Next. Making the End If = Next instead of course worked.

However, I still can't do this without an error anymore, so I have to go through and remove it all from my code:


Method Whatever()

   Local i:Int = 0

   For i = 0 to 10
      counter = counter + 1
   Next i

End Method



IT says there is an error on the "Next i" line, and removing the "i" makes it work.

I do wish I could still have the "Next i", but changing it to the following ultimately serves the exact same purpose:


Method Whatever()

   Local i:Int = 0

   For i = 0 to 10
      counter = counter + 1
   Next ' i

End Method



Thanks again for calling me out on potentially bad code :)


tiresius(Posted 2014) [#4]
I thought putting "Next <whatever>" was a valid use of the language, and <whatever> was ignored. It is in other basic languages I've seen.
But I'm also using ver78... It would be too bad if this wasn't okay anymore I think it is a nice way to keep track of nested conditions.


benmc(Posted 2014) [#5]
@tiresius Yes, that's why I've always used it - going all the way back to QBasic days.

However, I may be wrong. I'm noticing that with really simple tests - everything is working fine, even the "Next i" But something is breaking it so that it gives be errors in other programs.

I'm not sure anymore. I'm starting to think something else is messed up in my programs, but I can't figure out why it's causing it to error on the "Next i", etc, lines.


tiresius(Posted 2014) [#6]
Well if you go back that far, I think QBasic required it. LOL

If you haven't yet, you could try Jungle IDE which has a pretty good monkey parser that can give reasons for obscure syntax failures.
I'll be upgrading to latest Monkey soon and will see if my project goes bonkers.


ImmutableOctet(SKNG)(Posted 2014) [#7]
Wait, you aren't thinking of 'While' and 'Repeat'/'Until', are you? Because Monkey wasn't supposed to allow what you were talking about, nor does it actually make any sense.

First of all, your loops generally should be encapsulated in their own functions, or at least manageable enough to be put into small-ish functions. A function that's too long will be more susceptible to L1 cache misses (Though, modern compilers handle loops very well). In the case of larger loops, this can be an issue.

But never mind the hardware specific stuff, what do you mean by "Keep track of nested conditions"? Do you mean keep the variables around between/after loops/cycles? If so, you should just define these things first (Outside of the loop). And if you want a variable to be local to the loop, then you use 'Local' inside of it.

The variable 'I' in the case of "For Local I:= 0 To 10" is a bit different. In that example, 'I' will usually (Or at least conceptually) be popped off the stack when the loop finishes; this means it won't be accessible anymore. Defining 'I' before the loop, then writing "For I = 0 To 10" will then result in the use of our local variable 'I', which we would need to define before the 'For' loop in order to use it (Otherwise an error would occur on compile-time).

So, what did QBasic do? Did it effectively allow multiple (Or at least one) variable from the context of the loop to be returned? That's a bit convoluted, but an interesting idea. Or was this a matter of knowing what loop you're ending (Similar to the optional "End INSERTNAMEHERE", for example 'For' loops have 'End For' as an option from what I remember. To this same effect, there's also shorthand for some of these, like 'Endif')?

If that's the case, you shouldn't need this "Next i" thing. A well structured program would usually keep track of this via functions and methods (And their return values / manipulation of objects).

I can only assume you were getting at the idea of knowing when a variable's scope ends. This is done for you if you use nested local variables. And if you don't want 'i'/'I' to be a local variable after your loop, just use the "For Local" technique (This will pop that variable off the stack when done). Doing this will allow you to reuse the name 'i' (Or 'I' if you chose that) without having to worry about anything. Technically, you could still redefine what 'i'/'I' is (The "For Local" setup lets you technically do this), but that's not the point here.

Here's a (Good enough) example of Monkey's 'For' loop functionality, as well as some documentation on how loops work in Monkey:


As a side-note: With most C-like languages, the 'Exit' keyword is commonly called 'break'. Monkey being a rather severe deviation from BASIC, it doesn't really call it 'break'.

Here's some extra references:

* My forum post about the ':=' operator.

* The official Monkey documentation's "Statements" section.


Also note that I did not touch on "Foreach", this is a bit different, as Monkey resolves these as 'While' loops on a lot of targets. And they function rather differently. The official documentation has a bit about this from what I remember.


benmc(Posted 2014) [#8]
I appreciate the great response, thank you. Mostly, I was using the "Next <whatever>" as a way of keeping my code easily readable. I'm actually finding that it's working in some of my code and not others, and where it fails, it seems that it's related to mistakes I made that used to get "forgiven" by the interpreter. It appears that "End" used to be good for just about anything, even in "Next" instances, and this has since-changed, forcing me to make corrections to my code that I SHOULD make anyway, because it needs to be right.

I wasn't aware of the := operator, this may come in very useful, thank you :)


therevills(Posted 2014) [#9]
Check this thread regarding the "Next i" issue:
http://www.monkey-x.com/Community/posts.php?topic=8771&post=91596&view=all


tiresius(Posted 2014) [#10]
I guess it was never intentional ... I'll just comment out the looping variable, similar what I've done in C/C++ to show what "}" is closing in long code blocks.