May 25, 2013

Moose Interface Pattern with parameter enforcement

Moose interfaces are problematic, for 2 reasons.

1. They are compile time, but runtime features such as attribute delegation could provide the interface (role ordering is the real problem here)
2. They don't ensure anything other than the method name.

I think this problem can be solved better by using around instead of requires Ordering of course still matters here as you can have multiple `around` modifiers on a method. This will throw an exception if method is missing or if the types passed in are not correct.

3 comments:

  1. I already use this patter with in combination with 'requires' one additional twist.

    I enclose all around statements in an if ($ENV{DEV_MODE}) {} block to be able to only turn on the validation of the input parameters when developing. This way I can skip costly checks in the production environment without changing the code when verifying that the code using the interfaces behave correctly by setting an environment variable.

    ReplyDelete
  2. I was considering something similar.

    ReplyDelete

No trolling, profanity, or flame wars :: My Blog, my rules! No crying or arguing about them.