Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a pure type the same as an immutable type?

Tags:

c#

.net

I was checking out the documentation of PureAttribute in MSDN and I was surprised that it can be applied at class level. I knew what a pure function is, but I had never seen it applied to a type. In the MSDN documentation it states the following:

Indicates that a type or method is pure, that is, it does not make any visible state changes.

(bolded for highlighting purposes)

So my question is, if a pure type does not make any visible state changes, is that the same as an immutable type? Are both terms, if applied at the type level, equivalent? If not, can someone show me an example of a pure type that is not immutable or viceversa.

like image 1000
InBetween Avatar asked Aug 27 '14 17:08

InBetween


1 Answers

No, those are not the same:

  • An immutable type could have methods that mutate another object and this would not be pure.

  • On the other hand a type could expose mutable fields and yet only have pure methods that do not modify those fields.

like image 102
Victor Victis Avatar answered Oct 23 '22 23:10

Victor Victis