Why don't oop languages have a 'read only' access modifier?

Every time I write trivial getters (get functions that just return the value of the member) I wonder why don't oop languages simply have a 'read only' access modifier that would allow reading the value of the members of the object but does not allow you to set them just like const things in c++.

The private,protected,public access modifiers gives you either full (read/write) access or no access.

Writing a getter and calling it every time is slow, because function calling is slower than just accessing a member. A good optimizer can optimize these getter calls out but this is 'magic'. And I don't think it is good idea learning how an optimizer of a certain compiler works and write code to exploit it.

So why do we need to write accessors, read only interfaces everywhere in practice when just a new access modifier would do the trick?

ps1: please don't tell things like 'It would break the encapsulation'. A public foo.getX() and a public but read only foo.x would do the same thing.

EDIT: I didn't composed my post clear. Sorry. I mean you can read the member's value outside but you can't set it. You can only set its value inside the class scope.

Answers


You're incorrectly generalizing from one or some OOP language(s) you know to OOP languages in general. Some examples of languages that implement read-only attributes:

  • C# (thanks, Darin and tonio)
  • Delphi (= Object Pascal)
  • Ruby
  • Scala
  • Objective-C (thanks, Rano)
  • ... more?

Personally, I'm annoyed that Java doesn't have this (yet?). Having seen the feature in other languages makes boilerplate writing in Java seem tiresome.


Well some OOP languages do have such modifier.


In C#, you can define an automatic property with different access qualifiers on the set and get:

public int Foo { get; private set; }

This way, the class implementation can tinker with the property to its heart's content, while client code can only read it.


C# has readonly, Java and some others have final. You can use these to make your member variables read-only.

In C#, you can just specify a getter for your property so it can only be read, not changed.

private int _foo;

public int Foo
{
    get { return _foo; }
}

Actually, no they aren't the same. Public foo.getX() would still allow the internal class code to write to the variable. A read-only foo.x would be read-only for the internal class code as well.

And there are some languages that do have such modifier.


C# properties allow to define read only properties easily. See this article.


Not to mention Objective-C 2.0 property read-only accessors http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProperties.html


In Delphi:

strict private
  FAnswer: integer;
public
  property Answer: integer read FAnswer;

Declares a read-only property Answer that accesses private field FAnswer.


The question largely boils down to: why does not every language have a const property like C++?

This is why it's not in C#:

Anders Hejlsberg: Yes. With respect to const, it's interesting, because we hear that complaint all the time too: "Why don't you have const?" Implicit in the question is, "Why don't you have const that is enforced by the runtime?" That's really what people are asking, although they don't come out and say it that way.

The reason that const works in C++ is because you can cast it away. If you couldn't cast it away, then your world would suck. If you declare a method that takes a const Bla, you could pass it a non-const Bla. But if it's the other way around you can't. If you declare a method that takes a non-const Bla, you can't pass it a const Bla. So now you're stuck. So you gradually need a const version of everything that isn't const, and you end up with a shadow world. In C++ you get away with it, because as with anything in C++ it is purely optional whether you want this check or not. You can just whack the constness away if you don't like it.

See: http://www.artima.com/intv/choicesP.html

So, the reason wy const works in C++ is because you can work around it. Which is sensible for C++, which has its roots in C.

For managed languages like Java and C#, users would expect that const would be just as secure as, say, the garbage collector. That also implies you can't work around it, and it won't be useful if you can't work around it.


Need Your Help

How to resolve "HTTP Error 411. The request must be chunked or have a content length." in java

java http httpurlconnection

I am using HttpConnect and trying to get some token from the server. But whenever I try to get the response, its always saying you have not set or problem with content length even I tried to set the

Why JSON does not need setXXX method?

java json jackson

As question mention, I am very curious, why when the deserialization happen, it does not need setter for the fields? Only getter is needed?