Mispelled keywords, functions or methods error

Monkey Forums/Monkey Bug Reports/Mispelled keywords, functions or methods error

EdzUp(Posted 2015) [#1]
Import mojo

Function Main:Int()
	New MainClass
	Return( 0 )
End

Class MainClass Extends App
	Method OnCreate()
	End
	
	Method OnUpdate()
		Local I:Int
		
		For I=0 To 50
		Nex						'obviously this should be next and show throw an error here for 'undefined method or function'
	End
	
	Method OnRender()
	End
End


As you can see I mispelled Next in this example but instead of throwing an error on the Nex it jumps to the next method ( OnRender) and gives the error there surely this is a bug unless there is a function somewhere called Nex?


ImmutableOctet(SKNG)(Posted 2015) [#2]
Alright, so basically it's using the 'End' from your 'OnUpdate' method as the 'Next'/'End' for the loop. The end result is "Method OnRender()" being a part of the 'OnUpdate' method's body. And of course, the compiler doesn't support nested functions or methods, so it produces an error. This isn't completely a bug, though. 'For' loops can be written with 'End', and without 'Strict', 'Nex' can be assumed as a function or method call, without an explicit definition from this stage of compilation. And it won't get to that stage without first throwing an error about the code's structure (For good reason).

I don't know why, but Mark doesn't force the use of parentheses for functions and methods without arguments. Ideally, this would be forced when 'Strict' is on, but it's not (Maybe because of properties...?). From there, using 'Strict' would be ideal, but it doesn't fix the problem. But if we really want to be technical here, the compiler's doing its job pretty well. It's reporting an unexpected token, thus showing the user the problem. It's not exactly a detailed error, though. If we were to use an explicit "End Method" in this case, it wouldn't "leak" at all; it would claim there was an unexpected token at the end of the method.

To avoid these errors in general, you can basically do one or more of the following:
* Use the explicit versions of "End".
* Follow the same pattern you're currently using (Which is fine enough as it is), and then simply make the bodies for your statements, loops, etc, beforehand.
* Do what you're already doing, but use this as a reference for similar errors down the road.

I doubt we're really going to get Mark to change the behavior of this kind of function/method call. Chances are, someone's code will eventually be broken because of such a change. To put it simply, the compiler supports using 'End' for loops.


EdzUp(Posted 2015) [#3]
While I agree with you in to some degree I do think that the parenthesis idea is a good one (waiting for resulting roasting about c etc). In some cases a misspelled keyworks sometimes takes you to the top line of the offending file and with some being 1200+ lines its not really helpful with more accuracy it would be brilliant :)


MikeHart(Posted 2015) [#4]
How about using Strict? That should have catch it.


EdzUp(Posted 2015) [#5]
Will have a look, I think the point is quite a few coders don't use strict early on, will whack it in there and see what happens :)


Samah(Posted 2015) [#6]
*cough* braces and semicolons *cough*


EdzUp(Posted 2015) [#7]
Well strict was already in that file but I added strict to all the other files and it seem OK now :)

I do like braces and semicolons tbh but it would break everyone's code now