www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - feature request: interfaces for properties

reply Henning Hasemann <hhasemann web.de> writes:
It would be nice if members would behave like properties, so one could enforce
them with interfaces.
For example:

interface IHasFoo {
  int foo();
  void foo(int);
}

// This class fullfills the interface
class F1 : IHasFoo {
  int foo() { return 5; }
  void foo(int f) { };
}

// Would be nice to have this working too
class F2 : IHasFoo {
  int foo;
}

If thats too strange to you, maybe one could instead allow members
for interfaces like:

interface IHasFoo {
  int foo;
}

with F1, F2 fullfilling it.

I know that can be fixed with writing property methods around int foo,
but that looks a bit rediculus and requires renaming of that member.

Henning

-- 
v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR
hackerkey.com
Mar 06 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Henning,

 It would be nice if members would behave like properties, so one could
 enforce
 them with interfaces.
 For example:
 interface IHasFoo {
 int foo();
 void foo(int);
 }
 // This class fullfills the interface
 class F1 : IHasFoo {
 int foo() { return 5; }
 void foo(int f) { };
 }
 // Would be nice to have this working too
 class F2 : IHasFoo {
 int foo;
 }
 If thats too strange to you, maybe one could instead allow members for
 interfaces like:
 
 interface IHasFoo {
 int foo;
 }
 with F1, F2 fullfilling it.
 
 I know that can be fixed with writing property methods around int foo,
 but that looks a bit rediculus and requires renaming of that member.
 
 Henning
 

This is an interesting idea. To make it work however, the compiler would in effect have to write the getters and setters for you (not hard but somewhat inconsistent), it would just amount to syntactic sugar.
Mar 06 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
BCS wrote:
 Reply to Henning,
 
 It would be nice if members would behave like properties, so one could
 enforce
 them with interfaces.
 For example:
 interface IHasFoo {
 int foo();
 void foo(int);
 }
 // This class fullfills the interface
 class F1 : IHasFoo {
 int foo() { return 5; }
 void foo(int f) { };
 }
 // Would be nice to have this working too
 class F2 : IHasFoo {
 int foo;
 }
 If thats too strange to you, maybe one could instead allow members for
 interfaces like:

 interface IHasFoo {
 int foo;
 }
 with F1, F2 fullfilling it.

 I know that can be fixed with writing property methods around int foo,
 but that looks a bit rediculus and requires renaming of that member.

 Henning

This is an interesting idea. To make it work however, the compiler would in effect have to write the getters and setters for you (not hard but somewhat inconsistent), it would just amount to syntactic sugar.

Not to mention that, at the moment, the members would suddenly stop being valid lvalues. That reminds me; I need to finish that template for generating getters and setters :P -- Daniel -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 06 2007
prev sibling parent Henning Hasemann <hhasemann web.de> writes:
Okay, I didnt see that would harm consistency of the programming language.
I'd be interested in this template, though :-)

Henning

-- 
v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR
hackerkey.com
Mar 06 2007