Reassigning .ref and .meta? Rebinding class objects?

0 views
Skip to first unread message

Ingo Blechschmidt

unread,
Aug 5, 2005, 8:11:32 AM8/5/05
to perl6-l...@perl.org
Hi,

my $str = "Hello";
$str.ref = Int; # allowed?
$str.meta = &some_sub.meta; # allowed?


my $str = "Hello";
Str ::= Int; # allowed?
::Str ::= ::Int; # or is this allowed?
say $str; # still "Hello"? Or is it an Int now?

my $new_str = "Hi";
say $new_str; # is this an Int now?


Str ::= "simple string"; # not allowed?
::Str ::= "simple string"; # not allowed?


--Ingo

--
Linux, the choice of a GNU | Row, row, row your bits, gently down the
generation on a dual AMD | stream...
Athlon! |

Luke Palmer

unread,
Aug 5, 2005, 2:04:12 PM8/5/05
to Ingo Blechschmidt, perl6-l...@perl.org
On 8/5/05, Ingo Blechschmidt <ibl...@web.de> wrote:
> Hi,
>
> my $str = "Hello";
> $str.ref = Int; # allowed?
> $str.meta = &some_sub.meta; # allowed?

I hardly think those work. Both of those require a change of
implementation, which we can't do generically. So people would have
to specify how the implementation changes in that way, which they
generally won't/can't. You can do the first using user-defined
methods like so:

$str = $str as Int # $str as= Int ?

I'm not sure about the latter.

> my $str = "Hello";
> Str ::= Int; # allowed?
> ::Str ::= ::Int; # or is this allowed?

One of these two is probably allowed. But maybe it should be louder.

> say $str; # still "Hello"? Or is it an Int now?

That's a hard question. If say is implemented like so (this is
hypothetical; it will surely be implemented in terms of print):

multi say () { internal_put_str "\n" }
multi say (*$first, *@stuff) {
given $first {
when Str { internal_put_str $first }
when Int { internal_put_int $first }
...
}
say @stuff;
}

Then the first case becomes equivalent to "when Int", which would call
internal_put_str with an Int, which could be very dangerous. This is
why rebinding names globally is a bad idea. And in that case, I don't
know how or whether we should provide the ability.

Globally subclassing, however, isn't so dangerous:

Str does role {
method blah () {...}
};

But then comes to your question:

my $foo = "hello";
Str does role {
method blah () {...}
};
$foo.blah; # allowed

That is, do existing Strs all "does" the new role as well?

Luke

Reply all
Reply to author
Forward
0 new messages