seeking advice: possible new @attribute to mark class' default property to avoid alias this ?

someone someone at somewhere.com
Sun Aug 8 04:16:07 UTC 2021


On Sunday, 8 August 2021 at 00:57:47 UTC, Paul Backus wrote:
> On Sunday, 8 August 2021 at 00:52:43 UTC, someone wrote:
>> Now that I am aware of Walter's stance on alias this:
>>
>> "alias this has turned out to be a mistake" @ 
>> https://news.ycombinator.com/item?id=28029184
>>
>> ... would you, I mean the community, think is it a good idea 
>> to file a DIP to eventually get a new attribute to 
>> unambiguously label a class' default property ?
>
> Can you please explain what a "default property" is? I have 
> never encountered such a thing in any other programming 
> language.

```d
public class cComputer {

    private string pstrID;
    private string pstrName;

    public string ID() { return pstrID; }
    public string name() { return pstrName; }

    public void ID(const string lstrID) { pstrID = lstrID; }
    public void name(const string lstrName) { pstrName = lstrName; 
}

    this(const string lstrID, const string lstrName) {

       pstrID = lstrID;
       pstrName = lstrName;

    }

}

public class cComputers {

    private cComputer[string] pobjComputers;

    public cComputer[string] computers() { return pobjComputers; }

}

void main() {

    cComputer lobjComputer1 = new cComputer("one", "eins");
    cComputer lobjComputer2 = new cComputer("two", "zwei");
    cComputer lobjComputer3 = new cComputer("three", "drei");

    cComputers lobjComputers = new cComputers();

    lobjComputers.computers["один"] = lobjComputer1;
    lobjComputers.computers["два"] = lobjComputer2;
    lobjComputers.computers["три"] = lobjComputer3;

    assert(lobjComputers.computers["один"].ID == "one");
    foreach (cComputer lobjComputer; lobjComputers.computers) { 
writeln(lobjComputer.name); }

    /// now suppose cComputer default property is name
    /// now suppose cComputers default property is computers

    assert(lobjComputers["один"] == "one");
    foreach (cComputer lobjComputer; lobjComputers) { 
writeln(lobjComputer); }

    /// same output ... these are default properties; gotcha ?

}
```

PS: the above code was typed here but not actually tested


More information about the Digitalmars-d-learn mailing list