Illegal Expression Subtracting ArrayList Values

Monkey Forums/Monkey Programming/Illegal Expression Subtracting ArrayList Values

c.k.(Posted 2011) [#1]
What's the difference between

DrawText times.Get(times.Size()-1), 0, SCREEN_HEIGHT - 80

which works, and

DrawText times.Get(times.Size()-1) - times.Get(0), 0, SCREEN_HEIGHT - 80

which gives me the error "Error : Illegal expression type."

times is an ArrayList of Millisec() values.


GfK(Posted 2011) [#2]
Does the get() method return an int, or something else? In the latter case you'd need to cast the result to an integer before doing the subtraction.


therevills(Posted 2011) [#3]
Without seeing more of the code we would be only guessing... please post what times is (eg the class) and it's methods.


c.k.(Posted 2011) [#4]
therevills, is it not enough to say that times is an ArrayList() of Millisec() values? So you should know the class and methods. ;-)

Otherwise, I'll get you more info later today.


Jesse(Posted 2011) [#5]
try it by putting brackets around the computation like this:
DrawText (times.Get(times.Size()-1) - times.Get(0)), 0, SCREEN_HEIGHT - 80



c.k.(Posted 2011) [#6]
Jesse, thank you for the suggestion. I think I already tried that, but will try it for sure later when I get back to the project.


Floyd(Posted 2011) [#7]
therevills, is it not enough to say that times is an ArrayList() of Millisec() values?


No, it isn't. The real problem is that we don't know what the program is doing. We have only your statement of what you think it is doing. Thus we are reduced to guessing.

But I'll take a limited guess. Your statement about MilliSecs() suggests that times.Get() is an integer. The fact that you can't subtract these values suggests they are something else.

Edit: After a quick review of this thread I see this is just my verbose version of what Gfk said.


Jesse(Posted 2011) [#8]
I doubt it's the problem either. it was just a guess. I had similar problems with Blitzmax that is why I was guessing that.

And yes, you are going to have to put more of the code before we can make a conclusion. like how are you passing variables, what are the method parameters and how are you manipulating the array.


Samah(Posted 2011) [#9]
Size is and always has been a property. I believe Mark fixed the *bug* where you could call properties like a method. I'm on the bus so I can't test it :-)


marksibly(Posted 2011) [#10]
Hi,

> What's the difference between
> DrawText times.Get(times.Size()-1), 0, SCREEN_HEIGHT - 80
> which works, and
> DrawText times.Get(times.Size()-1) - times.Get(0), 0, SCREEN_HEIGHT - 80

Not 100% sure 'coz I don't know how ArrayList works, but I suspect it's a side effects of using box objects.

Basically, the core problem is that while box objects can be automatically be converted to ints, float, strings, they don't actually have a numeric type themselves as they are just objects!

So the first DrawText works because Monkey can just convert the box object to a string. Since DrawText takes a string, and the box object has a ToString$() method, the compiler knows what to do, no problemo.

But in the second, Monkey needs to know the type of both the LHS and RHS of the subtraction in order to work out the resultant type. ie: if LHS and RHS are both ints, result is an int, if one is a float, result is a float. But the LHS is just an object - it doesn't have a numeric type at all, so the compiler is stuck and you get an error. Going Int(...Get(...)) around the LHS and RHS should fix it.

The new generics should make box objects more or less obsolete in time, as you can now have 'real' containers of ints, floats and strings. So '.Get()' will return an actual int, not a box object, solving this sort of thing.


Jesse(Posted 2011) [#11]

Size is and always has been a property



you mean Length?

[edit]
too late anyway.


Samah(Posted 2011) [#12]
you mean Length?

In ArrayList it's Size, not Length.

Since DrawText takes a string, and the box object has a ToString$() method, the compiler knows what to do, no problemo.

Change DrawText to accept Object? Then it could look for ToString$() on whatever object is passed. If it supports it, whoopee. If not, print empty string. Of course in Java, toString() is part of the base Object class and prints the heap reference by default. And of course it could autobox primitives.


Jesse(Posted 2011) [#13]
gotcha! Thanks.


c.k.(Posted 2011) [#14]
Here's how times is defined:

Global times:ArrayList<IntObject> = New ArrayList<IntObject>


then updated

times.AddLast( Millisecs() )


and it gets used in that DrawText() code above.

When I wrap the LHS and RHS in Int(), it works fine. I can even do this:

DrawText lastTime - Int(times.Get(0)), 0, SCREEN_HEIGHT - 80

and it works OK. lastTime is set when times.AddLast() occurs this way:

lastTime = times.Get(times.Size()-1)


To check my understanding, times.Get() was returning an object, not an integer, and so the subtraction couldn't work?

THANK YOU GUYS for all the input. I don't know what a "box object" is, however. :)


Samah(Posted 2011) [#15]
For now, use IntArrayList and GetInt.
These will (hopefully) go away soon, and you can just use ArrayList<Int> with no problems.