Is defining get/set methods for every field overkill?

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Mon Nov 21 11:23:20 UTC 2022


On Monday, 21 November 2022 at 11:20:31 UTC, Alexandru Ermicioi 
wrote:
> On Thursday, 17 November 2022 at 04:39:35 UTC, thebluepandabear 
> wrote:
>> [...]
>
> Dunno if someone mentioned, but you can minimize use of 
> boilerplate by hiding it into mixin templates. Say you have:
> ```D
> mixin template Property(T) {
>   private T subject_;
>
>   T Property() { return subject_; }
>   void Property(T value) { subject_ = value; }
> }
> ```
>
>
> The you can use it in your class to define properties of class:
> ```D
> class MyMegaPropertyClass {
>   mixin Property!(string) myFancyProperty;
> }
>
> auto c = new MyMegaPropertyClass()
>
> c.myFancyProperty = "indeed"
> ```
>
> The only issue is that, by using eponymous naming you also 
> block any access of underlying subject_ or any other 
> miscellaneous info that you may add.
>
> Best regards,
> Alexandru.

You can use mixin templates to also define contlstructors, or any 
other boilerplate that is buildable using meta programming 
capabilities in D.

It would be awesome to have smth like Lombok from java but in D 
using mixin templates.

Best regards,
Alexandru


More information about the Digitalmars-d-learn mailing list