Why are opCall's not implicitely assignable?

Stewart Gordon smjg_1998 at yahoo.com
Sun Sep 24 16:45:27 PDT 2006


Karen Lanrap wrote:
> Stewart Gordon wrote:
> 
>> Not if one is interested primarily in what it does, rather than
>> how it does it.  Which is the whole point of properties.
> 
> Why has this to be restricted to properties?
> What are properties at all?

A form of syntactic sugar designed to be used when it makes sense to the 
semantics of what is being done.

For example:

     class Matrix {
         ...
         uint width() { ... }
         uint width(int w) { ... }
         uint height() { ... }
         uint height(int h) { ... }
     }

then you can do something like

     Matrix m = new Matrix;
     m.width = 42;
     m.height = 105;
     writefln("%d %d", m.width, m.height);

The idea is that you are setting/getting the width and height of m, just 
as you would be if width and height were member variables of Matrix. 
Defining them as properties just enables more work to be done, e.g. to 
adjust the internal data structures to fit the new settings.

> Why is a member of a class that is a class with an opCall no property 
> of that class?

Because it's already a member variable.  There's no need for it to be a 
property as well.

> import std.stdio;
> class C{
>     int opCall( int p){
>         return 2*p;
>     }
> }
> 
> class D{
>     C c;
>     ~this(){
>         c=new C;
>     }
> }

What kind of destructor is this, eh?

> void main(){
>     auto d=new D;
>     auto i=d.c=2;
> }
> 
> 
> As you might notice, we are right back from where we started.

What is what you're trying supposed to achieve over

     class D {
         int c(int p) {
             return 2*p;
         }
     }

???

Stewart.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:-@ C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS- 
PE- Y? PGP- t- 5? X? R b DI? D G e++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.



More information about the Digitalmars-d-learn mailing list