Hi Eduard and all,
I am trying to pass an integer by reference, but I can't get it right. Could any one help me on this? What I tried works fine for arrays, but not for integers only.
in chuck, references *cannot* be made to primitive types (int, float,
dur, time), only to objects. in function calling, primitives are always
passed by value, and non-primitives are always passed by reference (like
in Java), regardless of the usage of @. However, the compiler should
have caught the fact that a function has been declared to take an 'int @'
as argument - this is a bug, which has been fixed now and will be
included in 1.2.0.4. Additionally, arrays are always considered objects,
including arrays of primitives.
as for achieving pass-by-reference for primitive types, one way is to wrap
them in an object and pass the object:
//----------------------------------------
// slightly modified
// (@ have been removed from func args)
//-----------------------------------------
// define class to wrap int
class TheInt
{
int value;
}
// instantiate it
TheInt ti;
// set any initial values
5 => ti.value;
// call modify value
ModifyValue( ti, 10 );
// check again
<<< ti.value >>>;
fun void PrintArray( int array[] )
{
for( 0 => int i ; i < 10; i++ )
<<<"a[",i,"] = ", array[i]>>>;
}
fun int IsInArray(int array[], int num, int length )
{
if( length > array.cap() ) array.cap() => length;
if( !length ) return 0; // array is still empty
for( 0 => int i; i < length; i++ )
{
if( num == array[i] )
{
<<