Front page | perl.perl6.language |
Postings from June 2006
Re: lvalue functions and lvalue parameters
Thread Previous
From:
Sam Vilain
Date:
June 20, 2006 21:18
Subject:
Re: lvalue functions and lvalue parameters
Message ID:
4498C8A2.8090005@vilain.net
Jonathan Scott Duff wrote:
>> sub cond(Bool $c, $a, $b) is rw {
>> if $c return $a else return $b;
>> }
>>
>> Will this fail because $a and $b are not rw? If so, will it fail at run-
>> or compile-time? What about this:
>>
> That looks like it should be a compile-time failure to me.
>
Depends on the interpreter of course, but ideally, yes, compile time.
>> sub cond(Bool $c, $a is copy, $b is copy) is rw {
>> if $c return $a else return $b;
>> }
>>
>> Is it allowed, and if so is the behaviour to return a writeable copy of
>> $a or $b? I imagine that in any case
>>
> I'd expect a compile-time failure here too, or at the very least a
> warning. Besides, returning a local copy each time hardly seems
> useful, except perhaps as a way to leak memory.
>
It doesn't have to be a real copy, that's up to the implementation to
apply whatever COW mechanism it likes. The type of $a and $b in this
case is compatible with the return type of the function, so it should be
fine.
This example is a bit useless - cond($x, $a, $b) = $foo won't store the
result anywhere. So this could be a warning along the lines of "useless
use of variable in void context" (but more tailored to this condition)
>> sub cond(Bool $c, $a is rw, $b is rw) is rw {
>> if $c return $a else return $b;
>> }
>>
>> will do what I want.
>>
> That is what I would expect too.
>
Right.
>> my Int $a is constant = 0;
>> my Int $b is constant = 0;
>> (cond(True, $a,$b))++;
>>
>
> We have a "constant" declarator now, so that would be
>
> constant Int $a = 0;
> constant Int $b = 0;
> (cond(True, $a,$b))++;
>
> and that should fail at compile time because the compiler knows that
> $a and $b are constant and can't be rw.
>
Indeed it should, with the same disclaimer as above re: compile/runtime
Sam.
Thread Previous