www.digitalmars.com         C & C++   DMDScript  

D - Properties

reply Vathix <vathix dprogramming.com> writes:
I don't think allowing properties to be used like a function and a 
variable interchangably is a good idea.

class Foo
{
   int bar  = 3;
   int baz() { return 3; }
}
Foo foo = new Foo;
int bat = foo.bar(); //not possible
int bam = foo.baz; //possible

I understand the reasoning but doesn't it seem inconsistent? Isn't one 
of the reasons for having properties to be able to change a variable 
into a property, and vice versa, without code breaking? One day I might 
want baz to be a variable, but someone else already looked at my code 
and saw it was a function so they added () to access it. Well, even if 
that wasn't one of the reasons for properties, it would still be nice to 
designate which functions are properties and not allow them to be 
accessed like a function:

class Foo
{
   int bar = 3;
   property int baz() { return 3; }
}
Foo foo = new Foo;
int bam = foo.baz(); //not possible! it's a property
int bay = foo.baz; //good

This also solves problems with the compiler, because at no time will it 
need to treat it like a delegate.


-- 
Christopher E. Miller
www.dprogramming.com
irc.dprogramming.com #D
Feb 12 2004
next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
Agreed. I'm also concerned about the foo-free approach to properties. I also
agree that a property should not be callable as a normal function.

"Vathix" <vathix dprogramming.com> wrote in message
news:c0havo$2s9f$1 digitaldaemon.com...
 I don't think allowing properties to be used like a function and a
 variable interchangably is a good idea.

 class Foo
 {
    int bar  = 3;
    int baz() { return 3; }
 }
 Foo foo = new Foo;
 int bat = foo.bar(); //not possible
 int bam = foo.baz; //possible

 I understand the reasoning but doesn't it seem inconsistent? Isn't one
 of the reasons for having properties to be able to change a variable
 into a property, and vice versa, without code breaking? One day I might
 want baz to be a variable, but someone else already looked at my code
 and saw it was a function so they added () to access it. Well, even if
 that wasn't one of the reasons for properties, it would still be nice to
 designate which functions are properties and not allow them to be
 accessed like a function:

 class Foo
 {
    int bar = 3;
    property int baz() { return 3; }
 }
 Foo foo = new Foo;
 int bam = foo.baz(); //not possible! it's a property
 int bay = foo.baz; //good

 This also solves problems with the compiler, because at no time will it
 need to treat it like a delegate.


 -- 
 Christopher E. Miller
 www.dprogramming.com
 irc.dprogramming.com #D
Feb 12 2004
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Vathix wrote:

[...]
 Isn't one of the reasons for having properties to be able to change a
 variable into a property, and vice versa, without code breaking?
[...] Currently no. Otherwise you should consider this as a bug: void p(real p){ printf("real\n");}; void p(int p){ printf("int\n"); } void main(){ p=1; p=1.0l; } So long.
Feb 12 2004