develooper Front page | perl.perl6.language | Postings from August 2001

Re: properties, revisited

Thread Previous | Thread Next
Damian Conway
August 7, 2001 15:50
Re: properties, revisited
Message ID:

Dan suggested:

   > The syntax for variable and value properties are going to be different, I 
   > think, I just can't remember what it's going to be. (I think the colon's 
   > involved, but isn't it always?)

I think you're now channelling my de specula, not Larry's de jure. :-)

In A2, Larry stated that both variable and value properties would be
specified using the C<is> keyword.

Subsequently, I drafted a private proposal that separated the property
notion into two distinct semantic concepts with distinct syntaxes. That
proposal was sent only to Larry, but I will now reproduce it here to
show my current thinking on the subject.



It seems that my property concept is becoming a little chunky.
So let me try again...

Suppose variables and subroutines had compile-time "traits"
whilst values had run-time "properties".

Subroutine and variable traits are specified in declarations via a colon
(i.e. they're colonic adverbs on the C<my>, C<our>, C<temp> declarator, whilst remaining
syntactically near-as-dammit to Perl 5 attributes):

        my $foo : constant Wheel(4) Drive('transmission') = 'RV';
        my %bar : persistent;
        sub marine : Wet lvalue {...}

At run-time, traits may be read-accessed via a pseudo-method call:

        if (%bar.persistent) { ... }

        print "$foo.Drive() to all $foo.Wheel() wheels\n";

or (unambiguously) via the C<trait> built-in, which returns a hashref:

        if (trait(%bar){persistent}) { ... }

        my %t =  trait $foo;
        print "%t{Drive} to all %t{Wheel} wheels\n";

Value properties are set at run-time via an C<is>:

        return 0 is true;

        %bar{Jonah} is Called('son of fish food');

and may be accessed via a pseudo-method call:

        if ($foo.true) { ... }

        print "His nickname was ", %bar{Jonah}.Called;

or unambiguously via the C<prop> built-in, which returns a hashref:

        if (prop($foo){true}) { ... }

        print "His nickname was $(prop(%bar{Jonah}){Called})\n";

So we now have:

        * backwards compatibility with Perl 5 attributes
        * no compile-time C<is> vs run-time C<is> confusion
        * no variable vs value ambiguity
        * polymorphic access to variable or value annotations (via methods)
        * monomorphic access to variable or value annotations (via builtins)

and, as a bonus:

        * the ability to put a trait specifier *anywhere* in a declaration
          (because it's an adverb), and
        * the ability to use both types of annotation in one statement:

                my $threshold : constant = 0 is true;
                my $threshold = 0 is true : constant;

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About