"UUIDs in 10 Languages" Part 5: C++, Scala, and Dart

    May 20, 2024
    10 min read
    Tutorial
    Code examples
    uuid
    distributed-systems

    UUIDs in 10 Languages — Part 5: C++, Scala, and Dart

    Welcome to the final installment of our UUID tour! In this part, we’ll dive into UUID implementations for C++, Scala, and Dart.

    Each of these languages offers unique tools and paradigms — from C++ performance, to Scala's functional JVM ecosystem, to Dart's UI-centric tooling for Flutter.


    ⚙️ C++: UUIDs in a Systems Language

    C++ does not include UUID generation in the standard library, but several robust libraries support it — most notably, Boost.

    🔧 Using Boost UUID

    Install Boost with UUID module (usually part of base Boost packages):

    cpp
    #include <boost/uuid/uuid.hpp>
    #include <boost/uuid/uuid_generators.hpp>
    #include <boost/uuid/uuid_io.hpp>
    #include <iostream>
    
    int main() {
        boost::uuids::uuid id = boost::uuids::random_generator()();
        std::cout << "UUID: " << id << std::endl;
    }

    📦 Boost Options

    • random_generator() → UUIDv4 (random)
    • name_generator(ns) → UUIDv5
    • string_generator() → Parse from string

    ✅ Best Practices

    • Use UUIDv4 for general uniqueness
    • Store as 16-byte array (uint8_t[16]) when compactness matters
    • Avoid reinventing the wheel — Boost handles spec compliance

    ☕ Scala: Functional Power on the JVM

    Scala runs on the JVM, and UUIDs are fully supported via java.util.UUID.

    🔧 Generate UUIDv4

    scala
    import java.util.UUID
    
    val id = UUID.randomUUID()
    println(s"UUID: $id")

    🧪 Parse and Validate

    scala
    val maybeUuid = try {
      Some(UUID.fromString("550e8400-e29b-41d4-a716-446655440000"))
    } catch {
      case _: IllegalArgumentException => None
    }

    🔁 Use in Play Framework

    In Play JSON:

    scala
    import play.api.libs.json._
    
    implicit val uuidFormat: Format[UUID] = Format(
      Reads.uuid,
      Writes { uuid => JsString(uuid.toString) }
    )

    ✅ Best Practices

    • Use UUIDs in models and case classes
    • Validate UUID input with Try(UUID.fromString(...))
    • Use UUID as primary keys in Slick or Quill with MappedColumnType

    🦄 Dart: UUIDs for Flutter and Beyond

    Dart supports UUIDs through third-party packages, especially useful in Flutter apps.

    🔧 Install UUID Package

    bash
    dart pub add uuid

    📦 Generate UUID

    dart
    import 'package:uuid/uuid.dart';
    
    void main() {
      var uuid = Uuid();
      var id = uuid.v4();
      print('UUIDv4: $id');
    }

    Supports:

    • uuid.v1() – Time-based UUID
    • uuid.v4() – Random UUID
    • uuid.v5(namespace, name) – Deterministic UUID

    ✅ Best Practices

    • Use UUIDv4 for randomness in client-generated IDs
    • Validate UUIDs before use with regex or uuid.validate()
    • Don't use UUIDs as secrets — treat them as opaque identifiers

    📘 Summary Table

    LanguageGenerationLibrary/ModuleNotes
    C++random_generator()Boost UUIDRequires linking Boost libs
    ScalaUUID.randomUUID()Built-in via JavaWorks with Play, Slick, Quill
    DartUuid().v4()uuid packageIdeal for Flutter/Dart apps

    Final Thoughts

    That wraps up our five-part tour of UUIDs in 10 languages!

    From system programming to functional paradigms to cross-platform UI dev, UUIDs show up everywhere — and each ecosystem brings its own tools and conventions.

    If you’ve followed along from Python to Dart, you now have a cross-stack understanding of UUID generation, validation, and safe usage.

    👋 Thanks for joining the series — and may your UUIDs always be unique (and properly formatted).

    Generate Your Own UUIDs

    Ready to put this knowledge into practice? Try our UUID generators:

    Generate a Single UUID

    Create a UUID with our fast, secure generator

    Bulk UUID Generator

    Need multiple UUIDs? Generate them in bulk

    Summary

    This article explores how to generate and manage UUIDs in C++, Scala, and Dart — with practical examples tailored to each language's ecosystem. Learn how to implement UUIDs safely and effectively in your systems.

    TLDR;

    Part 5 of the series walks through UUID usage in C++, Scala, and Dart with practical examples.

    Key takeaways:

    • C++ supports UUIDs through libraries like boost::uuid
    • Scala uses java.util.UUID from the JVM ecosystem
    • Dart supports UUIDs through the popular uuid package

    Whether you're building real-time apps or working in high-performance or functional environments, these UUID patterns will help you get it right.

    Cookie Consent

    We use cookies to enhance your experience on our website. By accepting, you agree to the use of cookies in accordance with our Privacy Policy.