October 31, 2005
On Mon, 31 Oct 2005 08:25:06 +0100, Ivan Senji wrote:

> I can live with method's being invoked as properties (although it also has some isues) but non-member methods being called in property style? Why?

Such methods are members, just not of classes or structs. They are members of the module that contains them. You can think of a module as a type of singleton object in that it contains its own methods and data but you can't derive new instances of them and you can't derive other modules from them.

> void resX(int x)
> {
> //do something complicated to change resolution
> }
> 
> and use it like this:
> resX = 1024;

In your example (assuming its in a file called screen_io.d) you can say

  screen_io.resX = 1024;

just like a class instance reference.


Another neat usage is similar to your idea. Imagine that initially you had a module-level variable called 'resX' and you built you application around that idea. Then later, you realized that it needs some sophistication. You change it into a property and (most) of your code doesn't need changing. You still get stuffed up by lvalue situations...

   someFunc( &resX );  // Fails with properties!


Example:

import std.stdio;

// Original code.
version(original)
{
    int resX;
}

// Enhanced code
version(enhanced)
{
int actualRes;
void resX(int x)
{
    if ( x > 1024 )
        actualRes = 1024;
    else if ( x > 0 )
        actualRes = x;
    else
        actualRes = 0;

}
int resX() { return actualRes; }
}

void main()
{
  screen_io.resX = 1024;
  writefln("Resolution is %d", resX);
  screen_io.resX = 1023;
  writefln("Resolution is %d", resX);
  screen_io.resX = 1025;
  writefln("Resolution is %d", resX);
  screen_io.resX = 0;
  writefln("Resolution is %d", resX);
  screen_io.resX = -21;
  writefln("Resolution is ", resX);
}


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
31/10/2005 6:27:07 PM
1 2
Next ›   Last »