Michael Lazzaro wrote:

Seriously, tho, I'm not sure I understand the constantness part.

sub foo($x = 1) {...} # A6 syntax

sub foo(?$x = 1) {...}



I read the above as saying $x is indeed constant, but if it's not explicitly placed by the caller, we're going to pretend the caller passed us a 1.

No. I read that as: $x is a constant variable. If the caller passes an argument (by position or name) then that argument is bound to $x. If they don't pass a suitable argument, then $x is *not* bound, but simply initialized to 1.



Likewise, I read

sub foo($x //= 1) {...}

as saying the value stored in $x is a constant, but if the caller passed an undefined value (or didn't pass anything at all), we're going to instead pretend they passed us a (still-constant) 1. I'm not sure why that violates any rules.(?)

//= means "assign to lvalue unless lvalue is defined". So I read that as:
$x is a constant variable. Bind the corresponding argument to $x and then assign 1 to it if its value is undef. But the container bound to $x is bound as a constant, so it *can't* be assigned to.


I guess we could say that, in a parameter declaration, //= means "Bind the lvalue (i.e. the parameter). If the bound value is undef, unbind the lvalue, and then initialize it with the rvalue".

But that's horribly complex.

Damian

Reply via email to