For loop variable can't be a field !!

Monkey Forums/Monkey Bug Reports/For loop variable can't be a field !!

Brucey(Posted 2015) [#1]
... or an array index !

Parser falls over with "unexpected =" error.

Strict

Class TA
    Field num:Int

    Method Init:TA()
        num = 69
        Return Self
    End Method
End

Class TC
    Field a:TA
End

Function Main:Int()

	local l:= New List<TA>
	l.AddLast(New TA().Init())
	l.AddLast(New TA().Init())
	
	Local c:TC = New TC

	For c.a = Eachin l
	    Print c.a.num
	Next
	
	
	local arr:TA[1]
	For arr[0] = Eachin l
	    Print arr[0].num
	Next

	Return 0
End Function


:o)


bitJericho(Posted 2015) [#2]
Agreed. Interesting stuff! I like your sig.


MikeHart(Posted 2015) [#3]
Its by design Mark said loooonnng time ago.


Brucey(Posted 2015) [#4]
Buggy by design. Cool !

Or perhaps I should say, Monkey gives you less freedom to code than BlitzMax?


DruggedBunny(Posted 2015) [#5]
Are you sure about this, MH? The only result on a search within Bug Reports for 'field' and 'marksibly' as user that seems to come close is this one, but it appears to be a different bug.

This was my search. Of course, it could be buried somewhere else within the forum...


skid(Posted 2015) [#6]
The documentation does suggest otherwise, from Programming Language Statements:

For [ Local ] IndexVariable = FirstValue To | Until LastValue [ Step StepValue ]
     Statements...
Next

...

If Local is not present, IndexVariable must be a valid, existing variable.



I think correct call is to hardcode a "Variable used in For loop must be local to scope of loop" error message to help new comers with this particular corner case. and of course revise the docs including the fact that the step value must be "statically evaluated".


tiresius(Posted 2015) [#7]
Curious what the purpose of a looping variable being an object field would be ? Usually (for me) these are throw-away values.


ImmutableOctet(SKNG)(Posted 2015) [#8]
@skid: The documentation does specify that 'StepValue' must be a constant value. So, either 'Const', or a numeric literal. Though, the language reference page looks outdated. The keyword reference is a bit more descriptive. Still, the docs can be pretty bad at times.


GW_(Posted 2015) [#9]
Curious what the purpose of a looping variable being an object field would be

ya, talk about a corner-case!


Nobuyuki(Posted 2015) [#10]
Looping through a member property of an instance variable would be a sneaky way to execute some code in the iterator. Probably not very transparent design, but to each their own, I guess? :V

That being said, if the iterator had to be local to the block's scope then why the heck do we always have to declare the scope in the first place? VB can infer that you want a block-level temp variable for the iterator without having to use a declarative keyword, and this works even when Option Explicit is enabled; I figure this would be the most common usage scenario, so now with this thread I wonder if the flexibility to use field and array variables was supposed to be there, just that no one ever bothered to really get their elbows dirty with it....

Also, in mx2, I hope the requirement for a constant StepValue will be dropped :)


Danilo(Posted 2015) [#11]
> Also, in mx2, I hope the requirement for a constant StepValue will be dropped :)

You, or MX(2), can always translate such a for-loop to a while-loop.


ImmutableOctet(SKNG)(Posted 2015) [#12]
@Danilo: That's true, but it's also a lot of extra boiler plate. 'Step' is already kind of weird implementation-wise, but if we're just targeting C++, there's no reason it has to be constant.


DruggedBunny(Posted 2015) [#13]



Curious what the purpose of a looping variable being an object field would be

ya, talk about a corner-case!



Guilty! I posted this in Brucey's BlitzMax thread... it was something I did many years ago and I've no idea why. Think I was trying to be clever...