Skip to content

Class invariants and rescue clauses #7

@estaylorco

Description

@estaylorco

First, thanks so much for a fantastic library!

I was an Eiffel programmer once upon a time and miss DbC. Babel Contracts is the closest I've seen to Eiffel's implementation, and the smoothest. I have already started adding contracts to my code.

Two additions to the library would be most welcome:

  • Class invariants
  • rescue (as Eiffel would call it)

For the former, imagine this: We place an invariant structure on the constructor of a class, and that invariant is then automatically applied to the beginning and end of each method call (although I admit that I'm not sure what to do about getters and setters). The invariant would not be applied to the constructor, of course, as the instance is still developing.

As for the latter, a rescue clause would allow us to do the following:

  • Restore the method to a stable state
  • Intercept the exception Contracts throws when the contract is violated, and then re-throw it, most likely with a new Error (and the violation as an inner error)
  • Introduce a violation() method for capturing the contract exception thrown by Contracts

Consider the following trivial example:

setStatus(status) {
   require: {
      status;
      typeof status === 'string';
   }

   this.status = status;

   ensure: {
      this.status === 'status';
   }

   rescue: {
      throw new Error('Status was not set', violation());
   }
}

Do you think these things are doable?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions