Builder: Tiny Utility Library to Add a Builder API to Classes

Vijay Nayar madric at gmail.com
Fri Jan 6 12:54:07 UTC 2023


On Friday, 6 January 2023 at 09:26:51 UTC, thebluepandabear wrote:
>>   .isActive(true)
>>   .build();
>> ```
>
> Good 👍
>
> how would I extend the builder methods?

The builder methods are automatically generated and go up the 
inheritance chain to capture all the fields in your class. (I 
assume that you are referring to inheritance when you say 
"extend"?)

Here is one of the unit-tests demonstrating this:

```
class A2 {
   int a;
   string b;
}

class B2 : A2 {
   int c;

   mixin AddBuilder!(typeof(this));
}

/// All inherited fields should be available from the builder.
unittest {
   B2 b2 = B2.builder()
       .a(3)
       .b("ham")
       .c(4)
       .build();

   assert(b2.a == 3);
   assert(b2.b == "ham");
   assert(b2.c == 4);
}
```


More information about the Digitalmars-d-announce mailing list