byKey and byValue: properties or methods?

Jonathan M Davis jmdavisProg at gmx.com
Fri Jan 20 01:15:24 PST 2012


On Friday, January 20, 2012 09:58:42 Marco Leise wrote:
> Ah, I meant to say that getters should not modify their object. When I see
> "a = abc.x[i]" I would be a little surprised to find that it changes the
> observable state of abc. The same goes for "a = b.length()". Now it is
> clearer, isn't is? :p

Yes. Generally, setters should be used to set rather than getters doing it one 
way or another, though I have no idea how either of your examples there would 
result in the getter changing anything. However, if a getter property returns 
a ref, then it's both a getter and a setter. But whether exposing the 
underlying variable like that makes sense depends on the property - generally 
not, I think.

The one major place where you essentially have a getter being able to modifer 
the original through its return value in C++ that I'm aware of is the 
subscript operator, and it has to in order to function like the built-in 
substript operator, so that's a special case.

- Jonathan M Davis


More information about the Digitalmars-d mailing list