On Thu, Aug 11, 2005 at 02:22:04AM +0800, Autrijus Tang wrote:
: According to S06:
: 
:     my $pi is constant = 3;
: 
: Is this a special form?

I believe any assignment to a declarator is potentially a special form,
or we can't get assignment to "has" and "state" to work right.

: If yes, what does it desugar to?
: 
:     my $pi is constant := 3;
:     my $pi is constant ::= 3;

In this case it desugars to

    my $pi is constant = 3;

:-)

In other words the assignment to a my turns into an ordinary assignment
that happens at runtime.  Hopefully, since $pi is marked constant,
it could be optimized to a binding in many cases, but that's not the
semantics, and the difference shows up on objects pretending to be
values that have to be copied under assignment.  If you don't want
a copy every time the "my" is instantiated you need to use := instead.

: If not a special form, should this work? 
: 
:     my $pi is constant;
:     $pi = 3;

That could be made to work by defining constant to mean you can assign
to it if it's undefined.  But then it gets a little harder to reason
about it if $pi can later become undefined.  I suppose we could
disallow undefine($pi) though.

: If yes, should this pass compilation?
: 
:     my $pi is constant;
:     $pi = 3 if (0|1).pick;
:     $pi = 4;

If it's a special form, it's a different special form from the assignment
case, and would have to revert to the allow-if-undefined semantics, which
is probably a halting problem for the compiler, and so would have to
defer to runtime to determine legality.  But maybe that's a feature.

: If not, should this work?
: 
:     my ($x is constant, $y is constant) = (1, 2);

Under the assumption that only

    my ($x,$y) is constant = (1, 2);

is the compile-time special form, the embedded is-constant declarations
would have to be turned into the run-time write-once checking kind of
declaration, if we allow those at all.

I suppose the late-binding view of reality is that all of these
are fundamentally run-time constraints on the second attempt to
set. where we can intuit some of those constraints at compile time.

I'm not sure what that view does to formal parameters though.  Does it
mean that this is legal:

    sub foo($a, ?$b) {
        $b //= 2;
    }
    foo(1);

even though $b is not marked "rw" but is implicitly "constant"?
But maybe this would be illegal:

    sub foo($a, ?$b = 42) {
        $b //= 2;       # compile-time error
    }
    foo(1);

on the theory that the = of the default construct is the same special
case as the = in

    my $b is constant = 42;

Larry

Reply via email to