Home > Uncategorized > Awesome and Custom

Awesome and Custom

While toying with roles to find a better example for our class tutorial I believe to have stumbled onto a nice idiom. And so I wrote:

role Unitish[$unit = fail('Please provide a SI unit quantifier as a Parameter to the role Unitish')]

What leads to the following error message when the role argument is missing.

Could not instantiate role 'Unitish':
Please provide a SI unit quantifier as a Parameter to the role Unitish
  in any  at gen/moar/Metamodel.nqp line 2441
  in any protect at gen/moar/stage2/NQPCORE.setting line 802
  in any specialize at gen/moar/Metamodel.nqp line 2428
  in any specialize at gen/moar/Metamodel.nqp line 2644
  in any compose at gen/moar/Metamodel.nqp line 3010
  in any generate_mixin at gen/moar/Metamodel.nqp line 1319
  in any  at gen/moar/Metamodel.nqp line 1235
  in any mixin at gen/moar/Metamodel.nqp line 1270
  in sub postfix:<s> at si.p6 line 12
  in block <unit> at si.p6 line 14

So instead of letting the compiler say: “Dude, the module author wants you to provide a Str!”, I can actually tell the user what the string should look like. The way I’m using it results in a runtime error. The does-operator can be executed at compile time, providing awesome error messages the way you want.

Advertisements
Categories: Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: