Make dur a property?
Jacob Carlborg
doob at me.com
Sat Jan 26 03:33:11 PST 2013
On 2013-01-25 20:27, Nick Sabalausky wrote:
> I agree in principle, but unfortunately "return zis variable" getters
> are often needed because no major language (that I know of) offers any
> other way to create data that's privately-writable and
> publicly-read-only - a very common need.
>
> Ie, that's the "something" that "is wrong already": the lack of a
> simple built-in "The public can only read this, but I can R/W it." to
> obviate an extremely common idiom.
In Ruby one would do:
class Foo
attr_reader :foo
end
This would create a getter and an instance variable. Internally one can
write directly do the instance variable, or create a setter:
class Foo
attr_reader :foo
private
def foo= (value)
# code
end
end
The equal sign indicates the method is a property. Not that
"attr_reader" is not a language feature, it's implemented in the core
library. There are also functions available for getter and both getter
and setter.
In D, I would like to see something like this:
@property(get, set) int a;
@property(get) int b;
@property(set) int c;
@property int d; // same as "a"
This would create a getter and/or setter and an instance variable. One
could also experiment with the protection attributes like this:
public @property(get, protected set) int a;
One way could affect the instance variable, the other way to affect the
getter/setter implementation.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list