foreach on empty set error

Usually when unexpected errors occur I just bang my head until it goes away.

This time I can't really figure out where I'm going wrong.

Code:
typedef float[int,int] matrix;
typedef float[int] vector;

int matrixHeight(matrix m){
 int c=0;
 foreach i in m if(i>c)c=i;
 return c;
}

matrix matrixAddRow(matrix m,vector v){
 int c=m.matrixHeight()+1;
 foreach j,f in v m[c,j]=f;
 return m.normalize();
}

Error occurs during the foreach statement in matrixHeight(), but only when called from matrixAddRow();
If I type it manually
Code:
> ash import<mathlib.ash> matrix m; m.matrixHeight();
it returns 0 as expected.

Relative error:
Code:
class java.lang.ClassCastException: net.sourceforge.kolmafia.textui.parsetree.Value cannot be cast to net.sourceforge.kolmafia.textui.parsetree.AggregateValue
 

Bale

Minion
Did you know that arrays are passed as call by reference, not call by value? (This means your function is working on the original array, not a local copy.) I wonder what would happen if you change matrixAddRow to type void and don't have it return anything...
 
Wait. Even so, with the code as is and the error where it is, I can't find the logic that would cause this particular error. No edits are being made to the matrix in any way prior to the crash.

EDIT: I think I found the error.

matrix m=m.someFunctionThatUsesM() crashes but
matrix m; m=m.sameFunction() works.

So because it's not properly initialized it's falling apart is my best guess here.
 
Last edited:

Bale

Minion
matrix m=m.someFunctionThatUsesM() crashes but
matrix m; m=m.sameFunction() works.

That would be a problem. Those lines are not equivalent. The first is like saying int a = a+3. You cannot use a variable until AFTER it has been declared.
 
That would be a problem. Those lines are not equivalent. The first is like saying int a = a+3. You cannot use a variable until AFTER it has been declared.

This is true. However by virtue of the lack of "unknown identifier" error it was rather hard for me to track down. By letting me think m was defined it hid the true error.

Is it just me or is it much easier to spot these things when they are written in their own file and not run via the gCLI's ash feature? Too much horizontal code.
 

Winterbay

Active member
Is it just me or is it much easier to spot these things when they are written in their own file and not run via the gCLI's ash feature? Too much horizontal code.

No, which is why I often write my code in a code-window and paste it into the CLI for testing... That way I at least have some way of seeing the structure :)
 
Top