[chuck-users] returning arrays from functions

eduard aylon eduard.aylon at gmail.com
Sat Oct 13 22:07:33 EDT 2007


Found out why I was getting "empty" arrays. It seems there is some  
inconsistency with the way array.size() behaves. This is,
- when an empty array is created, size() and cap() differ.
- when cells of an empty array are filled individually, size() and cap 
() differ
- when an array is created and cells' values are assigned with @=>  
operator, size() = cap().

So size() cannot be trusted to return the actual size of the array. I  
was using size() to loop thru my array which returned 0, and thus  
thought I had an empty array. Is this a bug? or is it supposed to  
work like this.? I remember there was a discussion in the past about  
this and believe we were told to use cap() instead of size(). I used  
size() just because of being used to other programing languages, so I  
think that if size is not to be used, it should be removed.

Eduard


code explaining it:

int a[3];
<<<"empty array.", "Checking if .cap == .size">>>;
<<<"a.size()=", a.size(), "a.cap()=", a.cap() >>>;
for( 0 => int i; i < a.cap(); i++ ) i => a[i];
<<<"filled array.", "Checking if .cap == .size">>>;
<<<"a.size()=", a.size(), "a.cap()=", a.cap() >>>;

<<< "array created by @=>">>>;
[1, 2, 3] @=> int b[];
<<<"b.size()=", b.size(), "b.cap()=", b.cap() >>>;


On Oct 14, 2007, at 12:04 AM, Cesare Marilungo wrote:

> eduard aylon wrote:
>> This seems something I should have done in the past, but I can't
>> figure out how to return an array from a function. Couldn't find
>> documentation nor examples.
>>
>> case 1: compiling error. This seems the more natural way to me.
>>
>> fun int return_array( int a[] )
>> {
>> 	return a;
>> }
>>
>> case 2: works, but the returned array is empty
>>
>> fun int[] return_array( int a[] )
>> {
>> 	return a;
>> }
>>
> Hi,
> case 2 works here:
>
> [ 1,2,3 ] @=> int anarray[];
>
> fun int[] return_array( int a[] )
> {
>     return a;
> }
>
> <<< return_array( anarray )[0] >>>; //prints: 1: (int)
>>
>> thanks,
>>
>> eduard
>> _______________________________________________
>> chuck-users mailing list
>> chuck-users at lists.cs.princeton.edu
>> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>>
>>
>>
> --
> www.cesaremarilungo.com
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



More information about the chuck-users mailing list