digitalmars.D.learn - Function Overloading
- Salih Dincer (66/66) Oct 31 2023 ```d
- Basile B. (13/36) Nov 01 2023 Yes. D constructors are not named but the current implementation
- Salih Dincer (3/9) Nov 02 2023 Yeah, it works! Thanks...:)
- Basile B. (6/19) Nov 02 2023 I'm no sure how this could be used IRL but keep well in mind that
- Gaurav Negi (24/90) Nov 03 2023 Can you modify your code with the below code I hope you can get
```d struct Calculate { int memory; string result; auto toString() => result; this(string str) { add(str); } this(int num) { add(num); } import std.string : format; void add(string str) { result ~= str.format!"%s + "; } void add(int num) { memory += num; add(num.format!"%s"); } } import std.stdio; void main() { // without constructor: Calculate c; c.add("x"); c.add(2); c.writeln; // x + 2 + // with constructor: c = Calculate("x"); c.add(2); c.writeln; // x + 2 + } ``` There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there are only 2. Without implementing a similar function again for each constructor; is it possible to do something like `alias add = this;`? ```d struct Calculate { int memory; string result; auto toString() => result; // alias add = this; import std.string : format; this(string str) { result ~= str.format!"%s + "; } this(int num) { memory += num; add(num.format!"%s"); } } ``` SDB 79
Oct 31 2023
On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote:[...] is it possible to do something like `alias add = this;`? ```d struct Calculate { int memory; string result; auto toString() => result; // alias add = this; import std.string : format; this(string str) { result ~= str.format!"%s + "; } this(int num) { memory += num; add(num.format!"%s"); } } ``` SDB 79Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct. Note that however this kind of internal details should not be used, even if chances that this name changes are low (old 2014 code that uses it still compiles and have never broke, phobos uses it too IIRC). Otherwise take care to copy construction, that wont work with the alias.
Nov 01 2023
On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote:Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct.Yeah, it works! Thanks...:) SDB 79
Nov 02 2023
On Thursday, 2 November 2023 at 11:17:42 UTC, Salih Dincer wrote:On Wednesday, 1 November 2023 at 20:04:52 UTC, Basile B. wrote:I'm no sure how this could be used IRL but keep well in mind that **this is not nice code**. My answer should be more interepreted as "yes that is technically possible". ("can do" -> yes VS "should do" -> no). You also have the `opCall()` option btw.Yes. D constructors are not named but the current implementation adds a name that is `__ctor`, so add ```d alias add = __ctor; ``` to you struct.Yeah, it works! Thanks...:) SDB 79
Nov 02 2023
On Tuesday, 31 October 2023 at 20:09:44 UTC, Salih Dincer wrote:```d struct Calculate { int memory; string result; auto toString() => result; this(string str) { add(str); } this(int num) { add(num); } import std.string : format; void add(string str) { result ~= str.format!"%s + "; } void add(int num) { memory += num; add(num.format!"%s"); } } import std.stdio; void main() { // without constructor: Calculate c; c.add("x"); c.add(2); c.writeln; // x + 2 + // with constructor: c = Calculate("x"); c.add(2); c.writeln; // x + 2 + } ``` There is a simple struct like the one above that is not related to reality. There can be more than 2 function overloading in it, but in our example there are only 2. Without implementing a similar function again for each constructor; is it possible to do something like `alias add = this;`? ```d struct Calculate { int memory; string result; auto toString() => result; // alias add = this; import std.string : format; this(string str) { result ~= str.format!"%s + "; } this(int num) { memory += num; add(num.format!"%s"); } } ``` SDB 79Can you modify your code with the below code I hope you can get what you are looking for. struct Calculate { int memory; string result; auto toString() => result; private void addCommon(string str) { result ~= str.format!"%s + "; } import std.string : format; this(string str) { addCommon(str); } this(int num) { memory += num; addCommon(num.format!"%s"); } } Thanks
Nov 03 2023