Small suggestion for default constructors

Salih Dincer salihdb at hotmail.com
Tue Jan 17 15:03:44 UTC 2023


On Tuesday, 17 January 2023 at 02:45:30 UTC, TheZipCreator wrote:
> For structs, it automatically generates a constructor, and it'd 
> be nice if you could autogenerate constructors for classes too, 
> kinda like this:

You might like monkyyy's withMe() mixin template...

**Source Link:** 
https://forum.dlang.org/post/ipgwqcnfxltoijavflro@forum.dlang.org
**Sample Print:** `S!(Letter)(ab, [10, 20], true)`
```d
auto withMe(string elements, Me, T...)(Me me, T arg){
     with(me) mixin(elements);
		return me;
}

enum { A = 97, B, C, D }
enum a = Letter(A);
enum b = Letter(B);

struct Letter {
     char letter = 96;
     alias letter this;
}

import std.stdio;
void main()
{
   struct S(T)
   {
     T[] word;
     int[] nums;
     bool view;
   }

   auto test = S!Letter().withMe!q{
	     word = [a, b];
	     nums = [10, 20];
	     view = true;
   };
   test.writeln;
}
```
SDB at 79


More information about the Digitalmars-d mailing list