[chuck-users] functions returning arrays

mike clemow michaelclemow at gmail.com
Thu Feb 23 15:59:40 EST 2012


Alternatively, you can implement this will the append operator:

fun float[] t(float x,float nn[]) {

    // assign with zero length
    float rr[0];

    for(0 => int n;n<nn.size();n++) {
        // append
        rr << (nn[n] + x );
    }
    return rr;
}

<<< t(4.5, [7.,8.,9.])[2] >>>;  // should print 13.5


http://michaelclemow.com
http://semiotech.org




On Thu, Feb 23, 2012 at 3:56 PM, mike clemow <michaelclemow at gmail.com> wrote:
>
> Yes, ditto to what Kassen said.  (I'm too slow... ;)
>
> fun float[] t(float x,float nn[]) {
>
>     float rr[nn.size()];
>
>     for(0 => int n;n<nn.size();n++) {
>
>         nn[n] + x => rr[n];
>
>     }
>
>     return rr;
>
> }
>
> <<< t(4.5, [7.,8.,9.])[2] >>>;  // should print 13.5
>
>
>
>
> http://michaelclemow.com
> http://semiotech.org
>
>
>
>
>
> On Thu, Feb 23, 2012 at 3:45 PM, Kassen <signal.automatique at gmail.com> wrote:
>>
>> On Thu, Feb 23, 2012 at 12:27:22PM -0800, Colin Sullivan wrote:
>> > David,
>> >
>> > I think the problem may be that you are returning an array that is local to
>> > your function and it is getting deallocated when the function returns.
>>
>> That's what I thought too, and that certainly can happen.
>> In that case a extra assignment (using @) can increase the reference
>> count, tricking the simple CC. That's not "good form" just a
>> workaround while some of that stuff is a bit dodgy.
>>
>> That's not the issue here though, at least not the primary one. The
>> primary issue here is that rr gets defined without a length and so
>> apparently doesn't get initiated. Editing the relevant line to read;
>>
>>  float rr[nn.size()];
>>
>> makes sure all floats are initiated and for my tests it works.
>>
>> I think the issue is not with returning, the reported error is
>> refering -I suspect- to the moment when we try to chuck the result of
>> the addition to the array location, the error just isn't indicating
>> that, obfuscating what went wrong.
>>
>> I hope that helps (I also hope it's correct ;-) )
>>
>> Yours,
>> Kas.
>>
>>
>> _______________________________________________
>> 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