On 1/26/06, Rob Kinyon <[EMAIL PROTECTED]> wrote:
> On 1/26/06, Stevan Little <[EMAIL PROTECTED]> wrote:
> > > If there is need to treat something as a Hash, then provide it with a
> > > postcircumfix<{}> and leave it at that. It's highly unlikely that you
> > > will want to add Hash-like behavior to something that already has a
> > > postcircumfix<{}> because it probably has that behavior already.
> >
> > Well this is in relation to how to deal with an object which is a
> > blessed p6hash, in which case you may or may not want to have a
> > ^Hash-like interface for it (you might even want to overload the
> > ^Hash-like interface too).
>
> [snip]
>
> > Now, in order for C<$self as Hash> to make sense, $self would have to
> > be coercable into a Hash in some way. If $self is a blessed p6array
> > this might not make that much sense, so we would die because the
> > coercion failed. However, if $self is a blessed p6hash, then it would
> > make plenty of sense (IMO at least). It would allow us to get at the
> > underlying representation without having to sacrifice flexibility in
> > the class from which $self came. Basically you could do things like
> > this:
> >
> > class Golum;
> >
> > method new (Golum $class, Hash %params) {
> >     $class.bless(%params);
> > }
> >
> > method postcircumfix:<{}> (Golum $self, Any $key, Any $value) {
> >      die "Nasssty Hobbitses" if $value.does(Hobbit);
> >      $self as Hash {
> >         $self{$key} = $value;
> >     }
> > }
>
> How about just inheriting from Hash?
>
> class Gollum extends Hash;
>
> method postcircumfix:<{}> (Golum $self, Any $key, Any $value) {
>      die "Nasssty Hobbitses" if $value.does(Hobbit);
>     $self.NEXT.{}( $key, $value );
> }
>

Well, postcircumfix:<{}> is not just for emulating Hash :)

class struct;

has @.struct_def;
has $.struct_name;

method postcircumfix:<{}> ($class, Array of Pair @struct, $name) {
    $class.new(
        struct_def     => @struct,
        struct_name => $name
    );
}

Then you could do something like this maybe:

my $jedi_struct = struct{(
   first_name => 'Luke',
   last_name  => 'Skywalker'
)} = "jedi";

At least I assume you can, taking advantage of the "a class is
prototypical instance" thing to call postcircumfix:<{}> like that.

But I just might have had too much caffine at this point though ;P

Stevan

Reply via email to