[Perl5-syntax] How to test for a role in a method signature

Michael G Schwern schwern at pobox.com
Mon Mar 7 09:08:27 GMT 2011


On 2011.3.7 6:46 PM, Buddy Burden wrote:
> Now, I'm still concerned about Piers' point that, if you were to have
> MXMS code and you wanted to convert over to ours--and why wouldn't
> you? it's cooler, less buggy, and it runs faster--you really wouldn't
> want to have to wait and find out that you missed a "does coerce"
> until runtime.  So it occurs to me: I'm building my cache of type
> constraints at runtime, as they're called.  But I don't _have_ to do
> that, right?  Why not build the cache at compile-time, as the types
> are parsed?  That way, any unrecognized type gets reported
> immediately, at compile-time.  So your "does coerce" spits out an
> error such as:

The downside of resolving types at compile time is if a... whatever[1] isn't
loaded before your method compiles it will be treated as something you don't
expect.  And we don't want to force people to load classes at compile time
just so the types resolve at compile time.  That means they can't be run
selectively at runtime.

A simpler approach would be for MXD to special case "does coerce" in parsing.
 If it sees a role called "coerce" while parsing it warns.  Ta da!

To make this easier we probably want to move inject_from_signature() out of
parse_func() and extract the guts of parse_func() into parse_arg() which
handles parsing individual arguments.  Then MXD can override parse_arg() and
check if $sig->{does}{coerce}.

You can even preserve compatibility by special casing the handling of "does
coerce".  That would be a matter of splitting up inject_for_sig() into
individual methods as we discussed earlier.  Then MXD could override
inject_for_roles() and special case $sig->{does}{coerce} to use its current
meaning.

Any combination of the two could be applied depending on whether MXMS wants to
deprecate "does coerce" or just leave it as a special case.


[1] What are we calling these class/type/role things?

-- 
If at first you don't succeed--you fail.
	-- "Portal" demo



More information about the Perl5-syntax mailing list