[Perl5-syntax] Method::Signatures, now with types

Buddy Burden barefootcoder at gmail.com
Thu Mar 24 00:57:05 GMT 2011


Guys/Nick,

> But the problem I seem to be having is that MXMS _hasa_
> Context::Simple, whereas MS _isa_ Context::Simple.  So I can't figure
> out what to do with the context argument to parse().  In a fit of
> pique, I tried reblessing the context arg into MS, but that didn't
> work (no surprise there <g>).

Got it.

> My current code (with the probably ill-advised rebless) looks like this:
>
> package MXD_MS;
>
> use Class::MOP;
>
> sub import
> {
>    Class::MOP::load_class('MooseX::Declare');
>    Class::MOP::load_class('Method::Signatures');
>
>    my $meta = MooseX::Declare::Syntax::Keyword::Method->meta;
>    $meta->make_mutable();
>    $meta->add_around_method_modifier
>    (
>        parse => sub
>        {
>            my ($orig, $self, $ctx) = @_;
>
>            my $ms = bless $ctx, 'Method::Signatures';
>            $ms->parser;
>        }
>    );
>    $meta->make_immutable();
> }
>
>
> 1;

The problem is that this $ctx is not a
Devel::Declare::Context::Simple, but rather a MooseX::Declare::Context
which _contains_ a DDCS.  So the new code looks like this:

package MXD_MS;

use Class::MOP;

sub import
{
    Class::MOP::load_class('MooseX::Declare');
    Class::MOP::load_class('Method::Signatures');

    my $meta = MooseX::Declare::Syntax::Keyword::Method->meta;
    $meta->make_mutable();
    $meta->add_around_method_modifier
    (
        parse => sub
        {
            my ($orig, $self, $ctx) = @_;

            my $ms = bless $ctx->_dd_context, 'Method::Signatures';
            # have to sneak the default invocant in there
            $ms->{invocant} = '$self';
            $ms->parser($ms->declarator, $ms->offset);
        }
    );
    $meta->make_immutable();
}


1;

The other two problems I had to contend with were the fact that parser
(which comes from Devel::Declare::MethodInstaller::Simple) calls
init() on the DDCS object, so I had to provide the declarator and the
offset, so now it's just resetting those to what they already are
(which is totally unnecessary, but it's just assigning to a hash
slice, so it's not awful or anything), and then the second problem was
that I needed to provide a default invocant or else MS would parse the
signature like a "func" instead of like a "method".

Now, I'm still not that happy about the rebless, and I've hardcoded
the default invocant, which is going to have to change if we want to
allow the user to specify a different one (mayhap '$this' for the
C++-inclined), but the main thing is: it works. :)

So I can now do it outside of MXD.  I'm not sure of the best approach
for doing it (optinally) _inside_ MXD, but this is good enough for my
purposes.  Perhaps this is helpful for Nick if he wants to go ahead
and try to make this an option in MXD itself.


            -- Buddy



More information about the Perl5-syntax mailing list