Juerd,

On Jan 19, 2006, at 4:10 PM, Juerd wrote:
Stevan Little skribis 2006-01-19 15:45 (-0500):
  class Foo {
      method new ($class: %params) {
          $class.bless(%params);

Wouldn't that be %params.bless($class), or more directly,
%params.blessed = $class?

Nope, according to S12:

  As in Perl 5, a constructor is any routine that calls bless. Unlike
in Perl 5, you call it as a method on the class, passing the candidate
  as the first argument.

It then does on to give this code example:

  $object = $class.bless({k1 => $v1, k2 => $v2, ...});

In fact all example using &bless us it as a method of the $class.

This *won't* work the same in Perl 6 though. This is because,
what is blessed is not a literal "hash", but an instance of ^Hash.

The mistake here is that Foo doesn't "does Hash", I think.

But we cannot automagically inject a role into a class, for a number of reasons.

1) thats just plain evil
2) what if the role conflicts with other roles being does-ed by Foo? Debugging hell there. 3) What if Foo wants to have a .keys, .value, .exists, etc? Do they shadow the Hash version? What if Foo.keys is implemented using Hash.keys? Many issues abound here.

Sure, in Perl 5, you could have different kinds of references as
instances of the same class. But I don't recall ever having encountered
that.

bless([] => 'Foo');
bless({} => 'Foo');
bless(\*Foo => 'Foo');
bless(\(my $var) => 'Foo');

It silly, but you could do it. However this is not really related to my point. The issue I am describing looks more like this in perl 5:

  package Hash;
  sub new { ... }
  sub keys { ... }
  sub values { ... }
  sub exists { ... }

  package Foo;

  sub new {
      my ($class, $hash) = @_;
      ($hash->isa(Hash)) || die "hash needs to be an instance of Hash";
      bless($hash, $class);
  }

Why would you ever want to do such a think in Perl 5? So why would that be how &bless works in Perl 6?

Stevan

Reply via email to