18

I understand that the PureAttribute is used to mark something (class, method, delegate etc.) as making no visible changes, but I can see from the following definition that it can be applied to method parameters:

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Parameter|AttributeTargets.Delegate, AllowMultiple = false, Inherited = true)]
public sealed class PureAttribute : Attribute

What is the purpose of this attribute being applied to a parameter, such as in the following:

public void SomeMethod([Pure]SomeClass theParameter)
{
}

Does it imply that SomeMethod should not use anything on theParameter which is not marked as [Pure], meaning we can ensure the instance of SomeClass visibly appears the same before and after the invocation of SomeMethod?

I have not seen the PureAttribute used in this way and was wondering if this is due to lack of support in code contracts or because of a misunderstanding of mine?

Lukazoid
  • 19,016
  • 3
  • 62
  • 85
  • Classes are reference types, the method could alter a field or property of *theParameter* and that change is observable outside of the method. The [Pure] attribute promises that the method doesn't do this. A substitute for the *const* keyword in the C and C++ languages. – Hans Passant Mar 05 '14 at 13:10
  • @HansPassant If you could put your comment as an answer I would be happy to accept it. It seems to be what I had assumed, however I could find nowhere which explicitly stated this, the MSDN only mentions `[Pure]` being applied to methods and types. Do you by any chance have a link to any documentation which clarifies this purpose when applied to parameters? – Lukazoid Mar 05 '14 at 13:18
  • @HansPassant If it's the method which is doing the change it shouldn't that be stated by applying a `PureAttribute` to `SomeMethod` instead of the parameter? – Albireo Mar 05 '14 at 13:24
  • @Albireo I think Hans is saying that the method guarantees not to invoke any impure methods on `theParameter`, however `SomeMethod` may make some impure changes itself, i.e. `theParameter` will have no changes but other things may. – Lukazoid Mar 05 '14 at 13:26
  • 2
    Applying [Pure] on the method means something else afaik, it promises that it won't change the observable state of the *this* object. I should write it up but don't have quite the time to do it properly right now. Maybe later. – Hans Passant Mar 05 '14 at 13:30
  • 1
    @HansPassant I thought the `[Pure]` attribute signified that it does not change any visible state of anything, hence it is safe to use the attributed method in a `Contract.Requires` or `Ensures` call (which may or may not be executed depending upon options). If it allowed some visible side affects on other things, the inclusion/exclusion of `Contract.Requires` at runtime could change the behavior of the application. – Lukazoid Mar 07 '14 at 13:55

1 Answers1

5

The PureAttribute, as you stated, indicates that a type or method is pure, that is, it does not make any visible state changes (taken straight from the MSDN article).

Maybe your SomeClass is not marked as pure because it can change the state, however it does not mean that everything in it is impure.

Maybe your SomeMethod doesn't use any of SomeClass impure methods, maybe it simply reads its properties (I'm assuming you're not performing impure action in property getters, otherwise you're evil), so its usage of SomeClass is pure.­­­­­

Community
  • 1
  • 1
Albireo
  • 10,977
  • 13
  • 62
  • 96
  • 2
    I wasn't the one to downvote. However as my question states I do understand the purpose of the `PureAttribute` and use it almost daily, what I am after is some clarification of its purpose when applied to a parameter. – Lukazoid Mar 05 '14 at 13:19
  • @Lukazoid yes, I initially misread your question and I've updated my answer. Did you read the initial version or the updated one? – Albireo Mar 05 '14 at 13:21