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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay