views:

266

answers:

9

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.

+9  A: 

Well some OOP languages do have such modifier.

Darin Dimitrov
A: 

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

tonio
+2  A: 

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; }
}
BoltClock
public int Foo {get; private set;} is another way of achieving this. Without the additional declaration
btlog
+2  A: 

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.

Franci Penov
It seems in C# readonly means you can still modify the variable in the class, but from the outside it is readonly (http://msdn.microsoft.com/en-us/library/acdd6hb7%28VS.71%29.aspx).
C# readonly fields can be assigned only once in the declaration or in the constructor.
Franci Penov
+9  A: 

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.

Carl Smotricz
Does Java's `final` count?
BoltClock
@BoltClock: Good point, but a final field doesn't have quite the same semantics as a read-only property would. The latter will usually allow the class a private back door to changing the underlying value while keeping the encapsulation secure against the outside, which can be a nice benefit. `final` is... final.
Carl Smotricz
If Java's `final` doesn't count then Scala's `val` shouldn't count either.
missingfaktor
Ah, but Scala gives you much more flexibility than Java. While `val` and `var` take care of the 90% default cases with minimal coding, you *can* define a read-only, privately mutable attribute `foo` by doing `def foo = privateFoo`. It's just hand-waving and syntax, but much more concise than Java.
Carl Smotricz
+5  A: 

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.

Marcelo Cantos
A: 

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

rano
A: 

In Delphi:

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

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

gabr
A: 

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.

jdv
That guy clearly doesn't know C++. It's true that you can cast away const, but it's incredibly rare to ever actually do so.
DeadMG
@DeadMG: If i search in google code search, I get 32000 hits for const_cast. As a benchmark: dynamic_cast gets almost 60000. So its not just me and Anders who think they need const_cast.
jdv
@jdv: Need is not equal to wanting information about it, or think you need it when you don't. The number of results in a search engine is a really, really irrelevant metric. If you grepped 5 million lines of code and found it occurred often, that would be different.
DeadMG