What were some of your biggest breakthroughs while learning D?

Dylan Graham dylan.graham2000 at gmail.com
Fri Jul 9 19:35:20 UTC 2021


On Tuesday, 6 July 2021 at 20:53:12 UTC, Dylan Graham wrote:
> [Inspiration from this r/C_Programming 
> post](https://www.reddit.com/r/C_Programming/comments/oeoq82/what_were_some_of_your_biggest_breakthroughs/)
>
> What's something you learnt or realised, a habit you developed, 
> something you read or project you worked on that helped 
> accelerate your understanding and/or productivity in D?
>
> For example, mixin templates finally clicking and you realise 
> how to use them in your code base. Stuff like that.

- Mixin templates
At first it looked like some weird template wizard magic, but I 
now use them often. I've found them to be phenomenal in promoting 
code reuse in classes and structs. They're the backbone for the 
CAN bus implementation in my projects.

     ```D
     mixin template canbusImpl(alias packetID) {
       enum id = packetID;

       static assert(id > 512 && id < 1024, "IDs must be between 
512 and 1024!");

       void read(const ubyte[] bytes) {
         // read bytes from the array to set the values of the 
encapsulating struct
       }
       void write(ubyte[] bytes) {
         // convert the encapsulating struct into a byte array
       }
     }

     struct HeartbeatPacket {
       mixin canbusImpl!10;

       ubyte deviceID;
       ubyte status;
     }

     struct DeviceErrorPacket {
       mixin canbusImpl!11;

       ubyte deviceID;
       ushort code;
       short recoveryMethod;
     }

     if(canbus.incomingData.id == HeartbeatPacket.id) {
       HeartbeatPacket h;
       h.read(canbus.incomingData.bytes);
       writeln(h.deviceID ~ " has a heartbeat!");
     }
     ```

- UFCS
At first I was nervous about using it (mostly because I didn't 
want to get it wrong then have to go back and debug it), but once 
I really delved into some examples with it I definitely 
appreciated the feature. I wish I had mentioned them in my 
article :(

- Dlang Tour
Despite feeling more confident in D, I do sometimes come back to 
the tour to refamiliarise myself with certain concepts. It works 
well to pick things up quickly.


More information about the Digitalmars-d mailing list