www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - seeking advice: possible new attribute to mark class' default

reply someone <someone somewhere.com> writes:
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 ?

eg:  default string whatever(...);
eg:  default  property string whatever(...); /// if  property 
survives; that is

... or whatever you consider it should be named ?

Besides, alias this was not implemented to handle default 
properties albeit it seems it is common practice to (as I was 
previously advised to in a previous related post).

Do you think it'll be worth it ?


difficult to add it but—I can be quite wrong.


sword.
Aug 07 2021
parent reply Paul Backus <snarwin gmail.com> writes:
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.
Aug 07 2021
parent reply someone <someone somewhere.com> writes:
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
Aug 07 2021
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
So a field that will automatically be resolved to as part of the 
behavior of generated toString methods.

That really isn't what alias this is used for commonly. I.e.

struct ValueReference {
	private {
		SomethingElse* impl;
	}

	bool isNull() { return impl is null; }

	scope ref ValueType _get() { return impl.thingy; }

	alias _get this;
}

Only the problem is, this also works for classes and whole pile of extra 
cases.
Aug 07 2021
parent reply someone <someone somewhere.com> writes:
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 worked, but later on when reading manuals/posts/etc it was obvious alias this was something wholly different -that's why I got rid of all alias this on my code, not the least when I learned of Walter's stance on this one.
 struct ValueReference {
 	private {
 		SomethingElse* impl;
 	}

 	bool isNull() { return impl is null; }

 	scope ref ValueType _get() { return impl.thingy; }

 	alias _get this;
 }

 Only the problem is, this also works for classes and whole pile 
 of extra cases.
Aug 07 2021
parent reply jfondren <julian.fondren gmail.com> writes:
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); } ``` ```
Aug 07 2021
parent someone <someone somewhere.com> writes:
On Sunday, 8 August 2021 at 05:07:17 UTC, jfondren wrote:
 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'.
Probably.
 This is from Visual Basic, yeah?
Actually Visual FoxPro which had a full OOP implementation, for a lot of reasons hated VB back then, but yes, Microsoft family of developer tools have them and they were practical which is not to say they should be good, maybe they are a terrible idea, and *thats* why I was asking for advice beforehand.
 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);
 }
 ```
 ```
That I don't remember. But, if they were controversial back in the day ... I think it is all said and done ... ain't it ?
Aug 07 2021