Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Returing constant in interface implementation suggests impossible contracts #22

Closed
tom-englert opened this issue Mar 15, 2015 · 4 comments

Comments

@tom-englert
Copy link
Contributor

when implementing an interface method like this:

    bool ICollection.IsSynchronized
    {
        get
        {
            return false;
        }
    }

I get a CC warning:

Warning 12 CodeContracts: Consider adding the postcondition Contract.Ensures(Contract.Result<System.Boolean>() == false); to provide extra-documentation to the library clients

However since this is an interface implementation, I can't add contracts here.

@RxDave
Copy link

RxDave commented Mar 15, 2015

Adding a post-condition shouldn't be a problem. Did you try adding it? If you're getting another warning or an error then it may be a bug in Code Contracts.

The rule is: You can't add preconditions to inherited members, but you can add post-conditions. See Liskov Substitution Principle.

@aarondandy
Copy link
Contributor

While you can add post conditions I have found in my experience that CC seems to ignore them. Not sure if this is a bug or not.

@aarondandy
Copy link
Contributor

When I make contract classes (sorry if my assumption is wrong) for interfaces I get around these issues by using abstract override or abstract to signal that I don't want to add a contract for that method or property.

In this case you could probably just do abstract bool ICollection.IsSynchronized { get; } and leave it out of your contract class.

@tom-englert
Copy link
Contributor Author

You're right, adding a post condition works in this case. I expected to get the "x is implementing y, thus can't add contracts" warning - so I didn't try.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants