develooper Front page | perl.perl6.users | Postings from September 2018

Re: extending built-in classes

Thread Previous | Thread Next
From:
Curt Tilmes
Date:
September 22, 2018 21:40
Subject:
Re: extending built-in classes
Message ID:
CAMv20O9JwJ-FAEzHGVeQ7ukbFAJAPfykF=EB2kypaq5Nmj8TnQ@mail.gmail.com
On Sat, Sep 22, 2018 at 5:30 PM Larry Wall <larry@wall.org> wrote:

> On Sat, Sep 22, 2018 at 11:40:13AM -0700, Joseph Brenner wrote:
> : Sounds good, thanks.
>
> Well, yes, *sounds* good.  :-)
>
> Monkey patching is allowed but discouraged in Perl 6, because Ruby.
>

Mixed in roles:
    https://docs.perl6.org/language/objects#Mixins_of_roles
may be helpful as well.

You can add a role not just to a class, but to an instance of any other
class.

my $x = "something";
say $x.WHAT;
role MyDebugRole { method mydebugthing { say "mydebugthing" } }
$x does MyDebugRole;
say $x.WHAT;
$x.mydebugthing;

(Str)
(Str+{MyDebugRole})
mydebugthing

$x will totally act like it used to, but anywhere you like, now you can
call .mydebugthing() on it.

Pretty cool.

Curt

Thread Previous | 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