Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
unwrap_demo.cpp

Demonstrates improved unwrap() diagnostics with automatic source location.

Demonstrates improved unwrap() diagnostics with automatic source location.Shows how unwrap() on Result<T> and Optional<T> captures the call site via std::source_location, producing detailed error messages that include file name, line number, and function context. Also demonstrates safe alternatives: unwrap_or, is_ok/is_err checks, and monadic operations.

See also
kcenon::common::Result
kcenon::common::Optional
// BSD 3-Clause License
// Copyright (c) 2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <iostream>
#include <string>
using namespace kcenon::common;
// Helper function that returns an error
Result<int> divide(int numerator, int denominator) {
if (denominator == 0) {
return make_error<int>(
-1,
"Division by zero",
"math_module",
"Cannot divide by zero"
);
}
return ok(numerator / denominator);
}
// Function that returns None
if (id == 42) {
return Optional<std::string>("Alice");
}
return Optional<std::string>(std::nullopt);
}
std::cout << "=== Improved unwrap() Error Messages Demo ===\n\n";
// Demo 1: Result<T> unwrap with detailed error info
std::cout << "1. Result<T>::unwrap() with error:\n";
std::cout << "-----------------------------------\n";
try {
auto result = divide(10, 0);
// This will throw with detailed source location
int value = result.unwrap(); // Line will be captured automatically
std::cout << "Value: " << value << "\n";
} catch (const std::exception& e) {
std::cout << "Exception caught:\n" << e.what() << "\n\n";
}
// Demo 2: Optional<T> unwrap with source location
std::cout << "2. Optional<T>::unwrap() with None:\n";
std::cout << "------------------------------------\n";
try {
auto user = find_user(999);
// This will throw with source location
std::string name = user.unwrap(); // Line will be captured automatically
std::cout << "User: " << name << "\n";
} catch (const std::exception& e) {
std::cout << "Exception caught:\n" << e.what() << "\n\n";
}
// Demo 3: Safe alternatives (recommended)
std::cout << "3. Safe alternatives to unwrap():\n";
std::cout << "----------------------------------\n";
auto result = divide(10, 0);
// Option A: unwrap_or with default value
int value1 = result.unwrap_or(0);
std::cout << "unwrap_or(0): " << value1 << "\n";
// Option B: Check before accessing
if (result.is_ok()) {
std::cout << "Value: " << result.value() << "\n";
} else {
auto error = result.error();
std::cout << "Error: " << error.message << "\n";
std::cout << " Code: " << error.code << "\n";
std::cout << " Module: " << error.module << "\n";
}
// Option C: is_ok() check with value() access
if (result.is_ok()) {
std::cout << "Value via is_ok check: " << result.value() << "\n";
} else {
std::cout << "Result is in error state\n";
}
// Option D: Monadic operations
auto doubled = divide(10, 2)
.map([](int x) { return x * 2; })
.unwrap_or(-1);
std::cout << "Monadic map result: " << doubled << "\n";
std::cout << "\n=== Demo Complete ===\n";
}
int main() {
return 0;
}
int main()
Optional type similar to std::optional with Rust-like API.
Definition core.cppm:341
Result type for error handling with member function support.
Definition core.cppm:165
Core interfaces.
Definition adapter.h:21
Umbrella header for Result<T> type and related utilities.
Result< int > divide(int a, int b)
Optional< std::string > find_user(int id)
void demonstrate_improved_error_messages()