is it possible to have a default property for any given class ?

someone someone at somewhere.com
Mon Jun 7 15:26:27 UTC 2021


Consider the following code:

```d
class classComputer {

    private string pstrName;

    final @property string name() { return this.pstrName; }
    final @property void name(in string lstrName) { this.pstrName 
= lstrName; }

    this(
       string lstrComputerName
       ) {

       this.pstrName = lstrComputerName;

    }

}

class classComputers {

    classComputers lhs;
    classComputers rhs;

    int opApply(int delegate(classComputers) dg) { /// boilerplate 
code to handle the class's default collection

       if (lhs && lhs.opApply(dg)) return 1;
       if (dg(this)) return 1;
       if (rhs && rhs.opApply(dg)) return 1;
       return 0;

    }

    public classComputer[] computers; /// how can I tag this as 
the default property ?

}

void main (

    ) {

    classComputers lobjComputers = new classComputers;
    lobjComputers.computers ~= new classComputer("dell");
    lobjComputers.computers ~= new classComputer("ibm");
    lobjComputers.computers ~= new classComputer("apple");
    lobjComputers.computers[1].name = r"lenovo";

    foreach(lobjComputer; lobjComputers.computers) { 
writeln(lobjComputer.name); }

    ///foreach(lobjComputer; lobjComputers) { 
writeln(lobjComputer.name); } /// with default property (if 
possible)

}
```

The above code works correctly, however, avoiding the redundancy 
of lobjComputers.computers will be a plus.

Also tell me if the collection is implemented the right way, it 
is my first code using the opApply() delegate which I don't 
deeply understand for the time being.


More information about the Digitalmars-d-learn mailing list