rakudesign-by-contract

Does Perl 6 natively support Design by Contract?


It is listed as a language with native DbC support on the Wikipedia beside Eiffel and Spec#, but I can't find any mention whatsoever in the docs or in the test suite.


Solution

  • 2019 Update

    Imo, no.

    Because I don't think 6.d "implements most DbC features natively" for a reasonable definition of "most" I've removed it from the Wikipedia Design by Contract page.

    (If you think it should be put back in the native section despite this SO and my notes above and below, please make sure it appears in alphabetical order.)

    I think:

    A sketch of what I'm thinking follows.

    1. ORing preconditions and ANDing postconditions/invariants in the context of routine composition/inheritance/delegation:

    It is conjectured that PRE and POST submethods in a class could be made to run as if they were phasers in any public method of the class. This feature is awaiting further exploration by means of a ClassHOW extension.

    Original answer

    Check out Block Phasers, in particular the PRE and POST phasers. I haven't used them, and it's something like 25 years since I read the Eiffel book, but they look the part to me.

    The PRE and POST phasers are tested in S04-phasers/pre-post.t. I see at least one bug TODO.

    It would be wonderful if you would check out the doc, experiment with them (maybe using an online P6 evaluator), and report back so we can see what you think of them, hear if you encountered the TODO'd bug or any others, and decide what to do: