Why to have properties to sort or duplicate arrays ?
doob
doobnet at gmail.com
Sat Feb 3 09:59:37 PST 2007
Pierre Renié Wrote:
> Daniel Giddings Wrote:
> > if you make a method clean, it can act like a "property" even if it
> > doesn't return anything as it is a syntax "shortcut" if you like:
> >
> > import std.stdio;
> >
> > class C
> > {
> > void clean()
> > {
> > writefln( "C.clean" );
> > }
> > }
> >
> > void main()
> > {
> > C c = new C;
> > c.clean; // works, but is bad programming style
> > // c.clean() has a clearer meaning
> > }
> >
> > this doesn't mean you should remove this feature (or any other) because
> > bad programming style or design obfuscates the meaning of the code.
>
> Why not to add a keyword "prop" ?
> Like in this code :
>
> class C
> {
> void clean()//This is a method
> {
> writefln( "C.clean" );
> }
>
> prop int somevalue()//This is a property
> {
> return 1;
> }
> }
>
> void main()
> {
> C c = new C;
> c.clean; // won't work because there is no keyword "prop" (=> compile error)
> c.clean(); //correct
> int i = c.somevalue; // correct because this is a property
> }
>
> I want this because I want the compiler to prevent me from making mistakes.
I think it would be good to add a "property" keyword and maybe also "get" and "set". I think also would be nice to add a feature from ruby that i think works something like this:
Ruby code:
class C
{
attr_reader :variable1, :variable2, :variable3
}
and this will create a private variable and a read property. And ther is also "attr_writer" to create a write property. In D this could look something like this
D code:
class C
{
property
{
int variable1;
char[] variable2;
int variable3;
}
}
This would create a private variable and a get and set property method.
Then you could also write like this:
D code:
class C
{
private property
{
int variable1;
char[] variable2;
int variable3;
}
}
to make the get and set property methods private.
You could write like this:
D code:
class C
{
get
{
int variable1;
char[] variable2;
int variable3;
}
}
to only make a private variable and a get property method.
The get and set methods would be public as default and the variable would always be private. I think it would be great if you could write as i have described above because when you want a private variable and get and set methods that only sets or returns the value. If you would like to do something in the methods it could look like this:
D code:
class C
{
private int variable_;
public get int variable ()
{
return variable_;
}
public set int variable (int variable)
{
if (variable > 3)
return variable_ = variable;
}
}
More information about the Digitalmars-d
mailing list