DEV Community

Jesse Phillips
Jesse Phillips

Posted on

3 1

Add to a List in D

I thought this would be easy, considering I couldn't even provide a boolean it should be a surprise. D is understandably a different story. D provides array and associative arrays. These are generally sufficient and reaching for a container library isn't necessary.

Arrays utilize [] to denote the type and create literals.

string[] arrayVarName;
arrayVarName = ["array literal value"];
Enter fullscreen mode Exit fullscreen mode

Unlike every other language, D does not use addition to indicate concatenation.

arrayVarName ~= "additional value";
Enter fullscreen mode Exit fullscreen mode

This is often referred to as the append operator because arrays have reserved space where this might not cause an allocation.

This also works with array.

arrayVarName ~= ["more", "values"];
Enter fullscreen mode Exit fullscreen mode

Two lists can be combined with concatenation.

arrayVarName = ["value"] ~ ["more", "values"];
Enter fullscreen mode Exit fullscreen mode

And since D has operator overloading, user types these can have appropriately defined behavior on class or struct. Again this uses D powerful templates and is beyond this tutorial.

https://stackoverflow.com/questions/7826891/elegant-operator-overloading-in-d

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more