www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - question about property for built-in type

reply %u <djvsrose gmail.com> writes:
Hi,
I'm learning D right now and got a question about property.
I tried to add a property for built-in type like the following

 property bool equalZero(double a) { return a == 0.0; }

void main()
{
  ...
  double x = 4.4;
  bool isXZero = x.equalZero;
  ...
}

but got an error message
main.d(75): Error: no property 'equalZero' for type 'double'

I tried similar thing with int[] and it works.
Is that I did something wrong or property does not support built-in type like
double, int, real, ...?

Appreciate for your time and answer.
Oct 08 2010
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 08 Oct 2010 16:19:43 +0400, %u <djvsrose gmail.com> wrote:

 Hi,
 I'm learning D right now and got a question about property.
 I tried to add a property for built-in type like the following

  property bool equalZero(double a) { return a == 0.0; }

 void main()
 {
   ...
   double x = 4.4;
   bool isXZero = x.equalZero;
   ...
 }

 but got an error message
 main.d(75): Error: no property 'equalZero' for type 'double'

 I tried similar thing with int[] and it works.
 Is that I did something wrong or property does not support built-in type  
 like
 double, int, real, ...?

 Appreciate for your time and answer.

Uniform Function Call syntax (i.e. a.b(c) -> b(a, c)) only works for arrays atm. This may or may not be changed in future.
Oct 08 2010
parent %u <djvsrose gmail.com> writes:
Thanks for the reply.

I wonder are there any alternatives to achieve similar things for built-in
types?
I think this is very helpful for template function for built-in types.
Oct 08 2010
prev sibling parent Stanislav Blinov <blinov loniir.ru> writes:
  08.10.2010 16:19, %u wrote:
 Hi,
 I'm learning D right now and got a question about property.
 I tried to add a property for built-in type like the following

  property bool equalZero(double a) { return a == 0.0; }

 void main()
 {
    ...
    double x = 4.4;
    bool isXZero = x.equalZero;
    ...
 }

 but got an error message
 main.d(75): Error: no property 'equalZero' for type 'double'

 I tried similar thing with int[] and it works.
 Is that I did something wrong or property does not support built-in type like
 double, int, real, ...?

 Appreciate for your time and answer.

What you're trying to do is utilize uniform function call syntax (foo(T) === T.foo()), but that is currently supported *only* for arrays. AFAIK it's going to be implemented at some point, but right now you will have to use equalZero(a) instead of a.equalZero.
Oct 08 2010