views:

198

answers:

7

Which programming languages other than C++ support the concept of a constant class method? That is, what languages allow the programmer to constrain a method in such a way that it is guaranteed not to change the state of an object to which the method is applied?

Please provide examples or references in your answer.

+4  A: 

Haskell, since it's purely functional.

Actually, every value/method is constant in Haskell even though mutable state/IO can be modelled through a mathematical construct called monad.

Dario
There is no one correct answer to my question, but I've decided to accept this answer as "correct" because it is true and has so far received the most votes.
Derek Mahar
+3  A: 

All purely functional languages are all const by default because purely functional languages have no state to be changed.

Peter Alexander
...if by "allow", you mean "force" :)
Mike Seymour
What do you mean? I didn't use the word "allow".
Peter Alexander
No, but the question did.
Mike Seymour
You didn't use the word "allow", but I did in the original question which assumed that the default case is a "mutable" or "non-const" method. This is true of most object-oriented programming languages, but, as you point out, not true of functional languages.
Derek Mahar
+2  A: 

According to this Wikipedia entry, this feature is not available in many other object-oriented languages such as Java and C# or in Microsoft's C++/CLI.

Purely functional languages like Haskell, Curry, Ωmega do support *mandate* this feature.

missingfaktor
"Mandate" is an important point!
Derek Mahar
+2  A: 

ConstJava and Javari are two variations of Java that support the concept of a constant method. ConstJava has been obsoleted by Javari, though.

Derek Mahar
A: 

Perhaps you could write a custom attribute in .Net. The objects you pass in though, may all have to inherit from the same class i.e. EntityBase so you can manually ensure the state is the same.

Big Endian
+2  A: 

Since you tagged this as C++, I think you mean const method like this:

class A {
   int e;
public:
   int doSomething() const { 
      // ++ e;    // Compiler error, change data-member in read-only structure
      return e+1; // OK.
   }
};

(Although C++'s const is not a true-const because of the mutable members.)

Then I'm only aware of C++, D2, and all those functional languages supporting this.

  • C# doesn't support const methods but you can make all members readonly. You can also make a readonly wrapper class/subclass. Java doesn't have the const keyword, but like C# you can make all members final.
  • All functional languages use const correct methods by default because the functions are pure, but whether they support Object-oriented programming is another question.
KennyTM
Yes, I was referring in particular to C++ `const` methods.
Derek Mahar
Good point about `mutable` but bear in mind that you can also just `const_cast` `this` if you really want to in a so-called `const` member. :)
Troubadour
Although casting away constness is undefined behavior if the object is actually const, e.g. a global-scope const variable declaration.
dash-tom-bang
+3  A: 

I believe that Fortran (95 or greater I think) has what you are looking for. Coincidentally enough called "pure subroutines".

http://www.soks.org/view/Fortran95ForFortran77Programmers#pure_routines

http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=/com.ibm.xlf91a.doc/xlflr/pure.htm

miko
+1 you beat me to it by a minute or so. Now let's sit and watch the discussion on whether or not Fortran is OO. Should be fun.
High Performance Mark
"Pure" or "side-effect free" are two interesting ways to refer to `const` methods.
Derek Mahar
Though I had object-oriented languages in mind when I wrote the question, I avoided actually specifying object-oriented so that people would list many different classes of programming languages.
Derek Mahar
@Derek: `const`-method is not necessarily pure. For example, `void boo(int* x)const{*x=1;}` is obviously `const` but not pure.
KennyTM
@KevinTM, yes, good point.
Derek Mahar
A few years ago, I would have laughed at the concept of Fortran being OO. And then I did a huge project with it. I was blown away at what I could do to write OO code without all the OO support in the language. The biggest hangup I had was the lack of polymorphism. But I remember hearing a while back that Fortan 2000, the language of the future :), will have support for it.
miko
+1, but `this` would be modeled as an argument in Fortran and hence legal to change it within a pure method. - although using an index-as-`this` would match the concepts up nicely
Potatoswatter