Builder: Tiny Utility Library to Add a Builder API to Classes
thebluepandabear
therealbluepandabear at protonmail.com
Fri Jan 6 20:18:34 UTC 2023
On Friday, 6 January 2023 at 12:54:07 UTC, Vijay Nayar wrote:
> 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);
> }
> ```
I meant what if I want some extra behavior for the setter method
other than just assigning a value to the field. E.g. if I set a
password field, I might want to validate that it's valid.
More information about the Digitalmars-d-announce
mailing list