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

Jack jckj33 at gmail.com
Mon Jun 7 16:10:08 UTC 2021


On Monday, 7 June 2021 at 15:26:27 UTC, someone wrote:
> 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)

I think you meant to implement ranges? you can implement in the 
way you wanted: ```foreach(lobjComputer; lobjComputers)``` but 
the recommend approach is to get this array through the array 
index operator. It would go like this:

```d
class classComputer
{

    private string pstrName;

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

    this(string lstrComputerName = null) {

       this.pstrName = lstrComputerName;

    }

    classComputer lhs;
    classComputer rhs;

    int opApply(int delegate(classComputer) 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 ?

    auto opIndex() nothrow
    {
       return Range(computers);
    }

    protected static struct Range
    {
       private classComputer[] a;
       auto front() { return a[0]; }
       auto back() { return a[$ - 1]; }
       void popFront() { a = a[1 .. $]; }
       bool empty() { return a.length == 0; }

      size_t opDollar() { return a.length; }

      auto opSlice(size_t start, size_t end)
      {
          return a[start .. end];
      }
     }
}

void main ()
{

    import std.stdio : writeln;

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

    foreach(lobjComputer; lobjComputers[]) { 
writeln(lobjComputer.name); }

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

}
```


> }
> ```






More information about the Digitalmars-d-learn mailing list