Project 3: A Compile-Time CSV Parser

A function that takes a CSV file’s path as a compile-time string. This function will, during compilation, open the file, read its header, and generate a struct with fields matching the header names. The function’s return type will be an array of this compile-time-generated struct, and it will be populated with the CSV data.

Quick Reference

Attribute Value
Primary Language D
Alternative Languages N/A
Difficulty Level 4: Expert
Time Estimate 1-2 weeks
Knowledge Area Metaprogramming / CTFE
Tooling D’s CTFE engine
Prerequisites A solid grasp of D syntax from the first two projects.

What You Will Build

A function that takes a CSV file’s path as a compile-time string. This function will, during compilation, open the file, read its header, and generate a struct with fields matching the header names. The function’s return type will be an array of this compile-time-generated struct, and it will be populated with the CSV data.

Why It Matters

This project builds core skills that appear repeatedly in real-world systems and tooling.

Core Challenges

  • Executing code at compile time → maps to using static variables or template parameters to run functions during compilation
  • Reading a file during compilation → maps to using import(filename) which the compiler executes
  • Generating code as a string → maps to building a string like "struct CsvRow { int columnA; string columnB; }"
  • Using string mixins → maps to using mixin() to turn your generated string into actual, compiled code

Key Concepts

  • CTFE: “Programming in D” - Chapter 20.
  • String Mixins: Dlang.org documentation.
  • Templates: “D programming Language” by Andrei Alexandrescu - Chapter 5.

Real-World Outcome

// data.csv:
// id,name
// 1,Alice
// 2,Bob

// Your D code:
auto rows = loadCsv!("data.csv");
// The compiler generates `struct CsvRow { int id; string name; }`
// and `rows` is an array of `CsvRow`.

assert(rows[0].id == 1);
assert(rows[0].name == "Alice"); // This is fully type-checked!

Implementation Guide

  1. Reproduce the simplest happy-path scenario.
  2. Build the smallest working version of the core feature.
  3. Add input validation and error handling.
  4. Add instrumentation/logging to confirm behavior.
  5. Refactor into clean modules with tests.

Milestones

  • Milestone 1: Minimal working program that runs end-to-end.
  • Milestone 2: Correct outputs for typical inputs.
  • Milestone 3: Robust handling of edge cases.
  • Milestone 4: Clean structure and documented usage.

Validation Checklist

  • Output matches the real-world outcome example
  • Handles invalid inputs safely
  • Provides clear errors and exit codes
  • Repeatable results across runs

References

  • Main guide: LEARN_D_PROGRAMMING_LANGUAGE.md
  • “D programming Language” by Andrei Alexandrescu