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):
#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)
→ UUIDv5string_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
import java.util.UUID
val id = UUID.randomUUID()
println(s"UUID: $id")
🧪 Parse and Validate
val maybeUuid = try {
Some(UUID.fromString("550e8400-e29b-41d4-a716-446655440000"))
} catch {
case _: IllegalArgumentException => None
}
🔁 Use in Play Framework
In Play JSON:
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
dart pub add uuid
📦 Generate UUID
import 'package:uuid/uuid.dart';
void main() {
var uuid = Uuid();
var id = uuid.v4();
print('UUIDv4: $id');
}
Supports:
uuid.v1()
– Time-based UUIDuuid.v4()
– Random UUIDuuid.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
Language | Generation | Library/Module | Notes |
---|---|---|---|
C++ | random_generator() | Boost UUID | Requires linking Boost libs |
Scala | UUID.randomUUID() | Built-in via Java | Works with Play, Slick, Quill |
Dart | Uuid().v4() | uuid package | Ideal 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).