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

Piers Cawley pdcawley at bofh.org.uk
Mon Mar 7 10:08:24 GMT 2011


On Mon, Mar 7, 2011 at 9:08 AM, Michael G Schwern <schwern at pobox.com> wrote:
> 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.

Exactly my thought. I've certainly used class constraints on
parameters in classes that don't load said class because doing so
would create a circular dependency

> 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!

Works for me. Though should it still warn if, for some reason, there's
a role 'coerce' already loaded?

I like the proposal I saw floating around somewhere of using 'will
coerce' as the 'right' syntax for indicating coercion. It seems to fit
the place better than either 'is' or 'does'.



More information about the Perl5-syntax mailing list