Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
unwrap_demo.cpp File Reference
#include "kcenon/common/patterns/result.h"
#include <iostream>
#include <string>
Include dependency graph for unwrap_demo.cpp:

Go to the source code of this file.

Functions

Result< int > divide (int numerator, int denominator)
 
Optional< std::string > find_user (int id)
 
void demonstrate_improved_error_messages ()
 
int main ()
 

Function Documentation

◆ demonstrate_improved_error_messages()

void demonstrate_improved_error_messages ( )
Examples
unwrap_demo.cpp.

Definition at line 45 of file unwrap_demo.cpp.

45 {
46 std::cout << "=== Improved unwrap() Error Messages Demo ===\n\n";
47
48 // Demo 1: Result<T> unwrap with detailed error info
49 std::cout << "1. Result<T>::unwrap() with error:\n";
50 std::cout << "-----------------------------------\n";
51 try {
52 auto result = divide(10, 0);
53 // This will throw with detailed source location
54 int value = result.unwrap(); // Line will be captured automatically
55 std::cout << "Value: " << value << "\n";
56 } catch (const std::exception& e) {
57 std::cout << "Exception caught:\n" << e.what() << "\n\n";
58 }
59
60 // Demo 2: Optional<T> unwrap with source location
61 std::cout << "2. Optional<T>::unwrap() with None:\n";
62 std::cout << "------------------------------------\n";
63 try {
64 auto user = find_user(999);
65 // This will throw with source location
66 std::string name = user.unwrap(); // Line will be captured automatically
67 std::cout << "User: " << name << "\n";
68 } catch (const std::exception& e) {
69 std::cout << "Exception caught:\n" << e.what() << "\n\n";
70 }
71
72 // Demo 3: Safe alternatives (recommended)
73 std::cout << "3. Safe alternatives to unwrap():\n";
74 std::cout << "----------------------------------\n";
75
76 auto result = divide(10, 0);
77
78 // Option A: unwrap_or with default value
79 int value1 = result.unwrap_or(0);
80 std::cout << "unwrap_or(0): " << value1 << "\n";
81
82 // Option B: Check before accessing
83 if (result.is_ok()) {
84 std::cout << "Value: " << result.value() << "\n";
85 } else {
86 auto error = result.error();
87 std::cout << "Error: " << error.message << "\n";
88 std::cout << " Code: " << error.code << "\n";
89 std::cout << " Module: " << error.module << "\n";
90 }
91
92 // Option C: is_ok() check with value() access
93 if (result.is_ok()) {
94 std::cout << "Value via is_ok check: " << result.value() << "\n";
95 } else {
96 std::cout << "Result is in error state\n";
97 }
98
99 // Option D: Monadic operations
100 auto doubled = divide(10, 2)
101 .map([](int x) { return x * 2; })
102 .unwrap_or(-1);
103 std::cout << "Monadic map result: " << doubled << "\n";
104
105 std::cout << "\n=== Demo Complete ===\n";
106}
Optional< std::string > find_user(int id)
Result< int > divide(int numerator, int denominator)

References divide(), and find_user().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ divide()

Result< int > divide ( int numerator,
int denominator )

Definition at line 25 of file unwrap_demo.cpp.

25 {
26 if (denominator == 0) {
27 return make_error<int>(
28 -1,
29 "Division by zero",
30 "math_module",
31 "Cannot divide by zero"
32 );
33 }
34 return ok(numerator / denominator);
35}
Result< T > make_error(int code, const std::string &message, const std::string &module="")
Create an error result with code and message.
Definition utilities.h:91
VoidResult ok()
Create a successful void result.
Definition utilities.h:71

References kcenon::common::make_error(), and kcenon::common::ok().

Referenced by demonstrate_improved_error_messages().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_user()

Optional< std::string > find_user ( int id)
Examples
unwrap_demo.cpp.

Definition at line 38 of file unwrap_demo.cpp.

38 {
39 if (id == 42) {
40 return Optional<std::string>("Alice");
41 }
42 return Optional<std::string>(std::nullopt);
43}
Optional type similar to std::optional with Rust-like API.
Definition core.cppm:341

Referenced by demonstrate_improved_error_messages().

Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 108 of file unwrap_demo.cpp.

108 {
110 return 0;
111}
void demonstrate_improved_error_messages()

References demonstrate_improved_error_messages().

Here is the call graph for this function: