DevLabs > DevLabs Forums > Pex > Code contract on interface methods
Ask a questionAsk a question
 

General DiscussionCode contract on interface methods

  • Friday, November 07, 2008 8:40 PMLuis Miguel AbreuMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello guys,

    another quick question: should static analysis work against contracts defined for interfaces? here's a quick example:

    [ContractClass(typeof(PersonContract))]
        public interface IPerson
        {
            String FirstName { get; }
            String LastName { get; }
            void ChangeName(String firstName, String lastName);
        }

        [ContractClassFor(typeof(IPerson))]
        public class PersonContract:IPerson
        {
            public string FirstName
            {
                get { return CodeContract.Result<String>(); }
            }
            public string LastName
            {
                get { return CodeContract.Result<String>(); }
            }
            public void ChangeName(string firstName, string lastName)
            {
                CodeContract.Requires(firstName != null);
                CodeContract.Requires(lastName != null);
            }
        }

        public class Person:IPerson
        {
            private String _firstName;
            private String _lastName;

            public String FirstName
            {
                get { return _firstName; }
            }

            public String LastName
            {
                get { return _lastName; }
            }

            public void ChangeName(string firstName, string lastName)
            {
                _firstName = firstName;
                _lastName = lastName;
            }
        }

    Should the following code be caught during static analysis:

    var p = new Person();
    p.ChangeName(null, null);

    As you might guess, static analysis isn't picking this on my machine...

    thanks again

    Luis Abreu

All Replies

  • Monday, November 10, 2008 3:40 AMMike BarnettMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sigh. It should. I've fixed it and hope we can put out a new release soon. Sorry for the bugs! But I'm glad you are really trying it out.

    Currently the contract class needs to explicitly implement the interface methods, not implicitly as in your example. (But even if you do that, the released version won't work yet.) Do you think that is okay?

    Thanks!

    Mike
  • Wednesday, November 12, 2008 9:23 PMLuis Miguel AbreuMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello Mike.

    thanks for the tip.

    Since the contract class is only there for setting up the contract, I think that explicitly implementation won't be too much to ask for.

    thanks again and keep up the good work!

    PS: don't forget to update the docs. I think that there's nothing in the current pdf which mentions that the contract class must implement the interface explicitly.

    Luis Abreu