On 1/26/06, Rob Kinyon <[EMAIL PROTECTED]> wrote:
> On 1/26/06, Stevan Little <[EMAIL PROTECTED]> wrote:
> > Actually this might not be a bad approach in this case. Take this for 
> > instance:
> >
> > method foo (Foo $self, $key) {
> >     ((Hash) $self){$key}
> > }
> >
> > The syntax is ugly, but it makes what you are doing more explicit. I
> > would also think that in most cases this could be compile time checked
> > (if we can check $self's type (Foo), we can make sure Foo does/isa
> > Hash).
> >
> > Here are some other ideas for a typecasting syntax using "as" for the
> > keyword (which IIRC is taken for coercion??):
> >
> > $self as Hash {
> >      # $self is treated as a Hash inside the block
> >      $self{$key} = $value;
> > }
> >
> > # it could also return a wrapped
> > # $self for use in wider scopes
> > my $h = $self as Hash;
> > $h{$key} = $value;
>
> Isn't typecasting a DesignSmell? This kind of overloading is, imnsho,
> going to cause more coding bugs and is going to be considered a design
> flaw in P6.

General typecasting is probably a really really bad idea, I agree. But
some tightly controlled typecasting (shhh dont call it that or the
C/C++/Java programmers will freak out), might be useful in this case.
If you like, you can think of this as coercion, and not typecasting,
in fact this is probably the better word for it anyway.

> 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).

So, let me explain myself (hopefully) more clearly.

Lets say we treat infix:<as> as a general purpose coercion operator.
So that things like:

$num as Str;

will pretty much DWIM since a number can be easily coerced into a
string (at least 99% of the time, I am sure some of the math whizzes
will tell me different so I leave the 1% for that ;).

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;
    }
}

We could also do this with the p5hash representation too, assuming we
have a ^P5Hash of some kind (which is probably not a bad idea really).

Anyway, I now have some more caffine in my system (coffee,.. yummy),
not that it makes the idea any better, but at least it is more
detailed ;)

Stevan

Reply via email to