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

Re: Auto-creation of simple accessors (was: Perl 6 -

Thread Next
From:
Damian Conway
Date:
October 30, 2001 14:12
Subject:
Re: Auto-creation of simple accessors (was: Perl 6 -
Message ID:
200110302212.JAA27411@indy05.csse.monash.edu.au

John asked:

   > So, what about simple array accessors?

Writing:

	class Demo;

	my @.colors is public;

gives you:


   >     $obj.colors('red', 'green', 'blue');

No. $obj.colors() takes no arguments.


   >     $obj.colors = ('red', 'green', 'blue');

Yes.


   >     $obj.colors =  ['red', 'green', 'blue' ];

Yes. Arrays and array refs are (generally) interconvertable in Perl 6.


   >     push $obj.colors, 'red', 'green', 'blue;

Yes.


   >     pop $obj.colors;

Yes.


   >     print $obj.colors[1]

Yes.
   

   >     @{$obj.colors}[1]??

Yes (though you're missing the rest of the ternary operator there ;-)

   
   >     $obj.colors->[1]?? ;)

No. But "yes" to the Perl 6 equivalent:

	 $obj.colors.[1]

 
   > Can/will such an accessor be auto-created?

Yes.

 
   > How about hashes

Yes:

	class Demo;

	my %.fruit is public;


   > (or "pairs"?)

Pairs are just a scalar type:

	class Demo;

	my PAIR $.pear is public;


   >     $obj.fruit(apple => 'red', berry => 'blue');

No.


   >     $obj.fruit = (apple => 'red', berry => 'blue');

Yes.


   >     $obj.fruit = { apple => 'red', berry => 'blue' };

Yes. Hashes and hash refs are (generally) intraconvertable in Perl 6.


   >     # Change apple color, but leave the blue berry
   >     $obj.fruit(apple => 'green');

No. You want:

	$obj.fruit{apple} = 'green';


   >     print $obj.fruit('apple'); # green

No. You want:

	print $obj.fruit{apple};


   > Regardless of whether something like the above two examples can/will be
   > auto-created, what would those methods look like anyway?  Would they simply
   > be:
   > 
   >     method colors is lvalue { return @.colors }
   >     method fruit  is lvalue { return %.fruit  }

Close. They'd actually be:

        method colors is lvalue () { return @.colors }
        method fruit  is lvalue () { return %.fruit  }

I.e. explicitly take no arguments.


   > How many of the examples with the code above support?

Most of them, evidently.

Damian

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About