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

jfondren julian.fondren at gmail.com
Sun Aug 8 05:07:17 UTC 2021


On Sunday, 8 August 2021 at 04:51:48 UTC, someone wrote:
> On Sunday, 8 August 2021 at 04:30:12 UTC, rikki cattermole 
> wrote:
>> So a field that will automatically be resolved to as part of 
>> the behavior of generated toString methods.
>
> No. A default property can be another object altogether. The 
> best use case I can think of is a default collection for a 
> class such as lobjComputers.computers in my example.
>
>> That really isn't what alias this is used for commonly. I.e.
>
> I didn't know alias this even existed a month ago so I cannot 
> comment for what's oftenly used; I just stated that I was 
> pointed to alias this when I asked for default properties,

It might be that you were misunderstood. You're using "default 
property" as a term of art with a specific meaning, and the term 
itself looks generic enough that people can interpret it with 
their familiar meanings for 'default' and 'property'.

This is from Visual Basic, yeah?

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-declare-and-call-a-default-property

I've never run into like that before. And it doesn't seem 
well-loved in VB: "Because of these disadvantages, you should 
consider not defining default properties. For code readability, 
you should also consider always referring to all properties 
explicitly, even default properties."

This looks similar to that example:

```d
struct MsgBox {
     string[] contents;
     void opIndexAssign(string s, size_t i) {
         if (contents.length <= i)
             contents.length = i + 1;
         contents[i] = s;
     }
}

void main() {
     import std.stdio : writeln;

     MsgBox x;
     x[0] = "Hello";
     x[1] = " ";
     x[2] = "World";
     writeln(x.contents);
}
```
```


More information about the Digitalmars-d-learn mailing list