Elegant D

Peter C peterc at gmail.com
Wed Nov 26 05:34:18 UTC 2025


On Tuesday, 25 November 2025 at 01:18:21 UTC, Walter Bright wrote:
> I've been thinking about writing an article about what makes D 
> an elegant language to program in. D is indeed an elegant 
> programming language.
>
> I'm interested in anecdotes, experiences and examples of 
> Elegant D to incorporate!

// 1. Zero Runtime Allocation (High Performance)
// 2. Maximum Compile-Time Optimization (CTFE)
// 3. Safety and Control

// What this code does:
// Calculates the 64-bit integer overflow year (over 292 billion)
// using Compile-Time Function Execution (CTFE)
// and then prints the comma-formatted result at runtime
// with zero Garbage Collector (GC) allocation.

module myModule;
@safe:
private:

import core.stdc.stdio : printf;
import core.stdc.stdlib : exit;

enum MAX_BUFFER_SIZE = 26;

enum : double
{
     MAX_SECONDS = cast(double)long.max,
     SECONDS_PER_YEAR = 365.25 * 24.0 * 60.0 * 60.0
}

pure @nogc long calculateRawOverflowNumber()
{
     double maxYears = MAX_SECONDS / SECONDS_PER_YEAR;
     double targetYearDouble = 1970.0 + maxYears;
     return cast(long)targetYearDouble;
}

@trusted @nogc size_t longToCommaStringNogc(long n, char* buffer, 
size_t bufferSize)
{
     char[MAX_BUFFER_SIZE] temp;
     char* currentDigitPtr = temp.ptr + MAX_BUFFER_SIZE - 1;

     long currentN = n;
     size_t digits = 0;

     do
     {
         *currentDigitPtr = cast(char)('0' + currentN % 10);
         currentN /= 10;
         currentDigitPtr--;
         digits++;
     } while (currentN > 0);
     currentDigitPtr++;

     size_t commaCount = (digits - 1) / 3;
     size_t totalLength = digits + commaCount;

     if (totalLength + 1 > bufferSize)
     {
          printf("Error: Buffer too small.\n");
          exit(1);
     }

     char* targetPtr = buffer;
     for (size_t i = 0; i < digits; i++)
     {

         if (i > 0 && (digits - i) % 3 == 0)
         {
             *targetPtr = ',';
             targetPtr++;
         }

         *targetPtr = currentDigitPtr[i];
         targetPtr++;
     }

     *targetPtr = '\0';
     return totalLength;
}

@trusted @nogc void main()
{
     enum long RAW_OVERFLOW_NUMBER = calculateRawOverflowNumber();
     char[MAX_BUFFER_SIZE] stackBuffer;
     size_t len = longToCommaStringNogc(RAW_OVERFLOW_NUMBER, 
stackBuffer.ptr, stackBuffer.length);
     printf("The 64-bit overflow year is: %s\n", stackBuffer.ptr);
}



More information about the Digitalmars-d mailing list