digitalmars.dip.ideas - Avoid boilerplate code for member initialization
- Matthias =?UTF-8?B?Um/Dn215?= (26/26) Mar 29 Hi,
- Sergey (3/29) Mar 29 thanks
- Matthias =?UTF-8?B?Um/Dn215?= (2/4) Mar 29 Please explain why!
- Sergey (5/9) Mar 29 Check this slides and talk from last Dconf
- user1234 (12/22) Mar 29 Yes but we can imagine a special syntax, for example
- Kapendev (6/30) Mar 29 That is similar to how Dart does it, but without the type there.
- Matthias =?UTF-8?B?Um/Dn215?= (11/22) Mar 30 You are right, a special syntax is probably better than defining
- user1234 (5/8) Mar 30 Not necessarily because the type of the parameter can mismatch
- Matthias =?UTF-8?B?Um/Dn215?= (6/11) Mar 30 But then the implicit conversion could also be made when some
- user1234 (4/15) Mar 30 You're 100% right, I just wanted to the pinpoint the fact that to
- user1234 (6/9) Mar 30 In case someone would end up writing a DIP, I'd also like to
Hi,
code like that can be found in many projects:
class Person
{
string firstName;
string lastName;
this(string f, string l)
{
firstName = f;
lastName = l;
//do some more constructor logic
}
}
I suggest introducing a new keyword "member" which assigns the
members implicitly:
class Person
{
string firstName;
string lastName;
this(member firstName, member lastName)
{
//do some more constructor logic
}
}
That should be supported for constructors, and I think that
supporting it for functions would also be a good idea.
Mar 29
On Sunday, 29 March 2026 at 17:21:21 UTC, Matthias Roßmy wrote:
Hi,
code like that can be found in many projects:
class Person
{
string firstName;
string lastName;
this(string f, string l)
{
firstName = f;
lastName = l;
//do some more constructor logic
}
}
I suggest introducing a new keyword "member" which assigns the
members implicitly:
class Person
{
string firstName;
string lastName;
this(member firstName, member lastName)
{
//do some more constructor logic
}
}
That should be supported for constructors, and I think that
supporting it for functions would also be a good idea.
thanks
please no
Mar 29
On Sunday, 29 March 2026 at 18:17:07 UTC, Sergey wrote:thanks please noPlease explain why!
Mar 29
On Sunday, 29 March 2026 at 18:24:09 UTC, Matthias Roßmy wrote:On Sunday, 29 March 2026 at 18:17:07 UTC, Sergey wrote:Check this slides and talk from last Dconf https://dconf.org/2025/slides/korpel.pdf So the answer: adding this new keyword doesn't worth the value it is bringing to the language.thanks please noPlease explain why!
Mar 29
On Sunday, 29 March 2026 at 18:30:24 UTC, Sergey wrote:On Sunday, 29 March 2026 at 18:24:09 UTC, Matthias Roßmy wrote:Yes but we can imagine a special syntax, for example ```d class Person { string name; this(string this.name) { } } ``` or even just `.name`.On Sunday, 29 March 2026 at 18:17:07 UTC, Sergey wrote:Check this slides and talk from last Dconf https://dconf.org/2025/slides/korpel.pdf So the answer: adding this new keyword doesn't worth the value it is bringing to the language.thanks please noPlease explain why!
Mar 29
On Sunday, 29 March 2026 at 19:03:20 UTC, user1234 wrote:On Sunday, 29 March 2026 at 18:30:24 UTC, Sergey wrote:That is similar to how Dart does it, but without the type there. Having a bit of friction on this part is not that bad though. How often do you write ctors? And for types that are not vectors you will usually want more than to just set x to x and y to y. I say it's not worth the change.On Sunday, 29 March 2026 at 18:24:09 UTC, Matthias Roßmy wrote:Yes but we can imagine a special syntax, for example ```d class Person { string name; this(string this.name) { } } ``` or even just `.name`.On Sunday, 29 March 2026 at 18:17:07 UTC, Sergey wrote:Check this slides and talk from last Dconf https://dconf.org/2025/slides/korpel.pdf So the answer: adding this new keyword doesn't worth the value it is bringing to the language.thanks please noPlease explain why!
Mar 29
On Sunday, 29 March 2026 at 19:03:20 UTC, user1234 wrote:
Yes but we can imagine a special syntax, for example
```d
class Person
{
string name;
this(string this.name)
{
}
}
```
or even just `.name`.
You are right, a special syntax is probably better than defining
a new keyword. But the redundant type should be omitted:
class Person
{
string name;
this(this.name)
{
//some more constructor logic
}
}
Mar 30
On Monday, 30 March 2026 at 07:55:28 UTC, Matthias Roßmy wrote:[...] But the redundant type should be omitted: [...]Not necessarily because the type of the parameter can mismatch with the member's one but later, in the body, the auto-generated AssignExp can be semantically valid if there's a possible implicit conversion.
Mar 30
On Monday, 30 March 2026 at 10:48:22 UTC, user1234 wrote:On Monday, 30 March 2026 at 07:55:28 UTC, Matthias Roßmy wrote: Not necessarily because the type of the parameter can mismatch with the member's one but later, in the body, the auto-generated AssignExp can be semantically valid if there's a possible implicit conversion.But then the implicit conversion could also be made when some expression is converted to the parameter. I don't see any use case for having a different type for the parameter. And if it would be really needed in a very rare case, normal parameters with an assign expression in the body can still be used.
Mar 30
On Monday, 30 March 2026 at 11:22:15 UTC, Matthias Roßmy wrote:On Monday, 30 March 2026 at 10:48:22 UTC, user1234 wrote:You're 100% right, I just wanted to the pinpoint the fact that to specify the type is not always "redundant". Let's say that was just a detail.On Monday, 30 March 2026 at 07:55:28 UTC, Matthias Roßmy wrote: Not necessarily because the type of the parameter can mismatch with the member's one but later, in the body, the auto-generated AssignExp can be semantically valid if there's a possible implicit conversion.But then the implicit conversion could also be made when some expression is converted to the parameter. I don't see any use case for having a different type for the parameter. And if it would be really needed in a very rare case, normal parameters with an assign expression in the body can still be used.
Mar 30
On Sunday, 29 March 2026 at 17:21:21 UTC, Matthias Roßmy wrote:Hi, code like that can be found in many projects: [...]In case someone would end up writing a DIP, I'd also like to mention that this feature actually solves a whole little set of problems related to how parameters are named. For now this is handled using linters, for example this one check from PVS studio https://pvs-studio.com/en/docs/warnings/v3196/.
Mar 30









Kapendev <alexandroskapretsos gmail.com> 