|
| 1 | +--- |
| 2 | +description: Ox provides utility functions for common operations in direct-style code: `.pipe`, `.tap`, `.discard`, `uninterruptible` and `debug`. These are often inline methods with no runtime overhead. |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Ox Utility Functions |
| 7 | + |
| 8 | +Ox provides utility functions for common operations in direct-style code: `.pipe`, `.tap`, `.discard`, `uninterruptible` and `debug`. These are often inline methods with no runtime overhead. |
| 9 | + |
| 10 | +## Top-Level Utilities |
| 11 | + |
| 12 | +```scala |
| 13 | +import ox.{sleep, debug, uninterruptible} |
| 14 | +import scala.concurrent.duration.* |
| 15 | + |
| 16 | +// Scala-friendly sleep |
| 17 | +sleep(2.seconds) // Better than Thread.sleep |
| 18 | + |
| 19 | +// Debug printing with expression and value |
| 20 | +debug(computeExpensiveValue()) // Prints: computeExpensiveValue() = 42 |
| 21 | + |
| 22 | +// Uninterruptible code blocks |
| 23 | +uninterruptible { |
| 24 | + criticalCleanupOperation() |
| 25 | + updateImportantState() |
| 26 | +} // Cannot be interrupted even if thread/fork is cancelled |
| 27 | +``` |
| 28 | + |
| 29 | +## Extension Methods for Chaining |
| 30 | + |
| 31 | +```scala |
| 32 | +// Pipe - apply function and return result (useful for chaining) |
| 33 | +val result = getData() |
| 34 | + .pipe(processData) |
| 35 | + .pipe(validateData) |
| 36 | + .pipe(formatData) |
| 37 | + |
| 38 | +// Tap - apply function but return original value (useful for side effects) |
| 39 | +val user = fetchUser(userId) |
| 40 | + .tap(u => logger.info(s"Fetched user: ${u.name}")) |
| 41 | + .tap(auditService.logAccess) |
| 42 | + .tap(cacheService.store) |
| 43 | + |
| 44 | +// Discard - avoid "discarded non-unit value" warnings |
| 45 | +computeValue().discard // Explicitly discard result |
| 46 | +``` |
| 47 | + |
| 48 | +## Exception Handling Utilities |
| 49 | + |
| 50 | +```scala |
| 51 | +// Handle exceptions with side effects |
| 52 | +val result = riskyOperation() |
| 53 | + .tapException(e => logger.error(s"Operation failed: ${e.getMessage}")) |
| 54 | + .tapNonFatalException(e => metrics.incrementErrorCounter()) |
| 55 | +``` |
| 56 | + |
| 57 | +## Future Integration |
| 58 | + |
| 59 | +```scala |
| 60 | +import scala.concurrent.Future |
| 61 | + |
| 62 | +// Block on Future completion (direct-style integration) |
| 63 | +val futureResult: Future[String] = asyncOperation() |
| 64 | +val result: String = futureResult.get() // Blocks until complete |
| 65 | +``` |
| 66 | + |
| 67 | +## Real-World Examples |
| 68 | + |
| 69 | +```scala |
| 70 | +// Data processing pipeline with utilities |
| 71 | +def processUserData(userId: String): ProcessedUser = { |
| 72 | + fetchUser(userId) |
| 73 | + .tap(u => debug(u)) // Debug log the user |
| 74 | + .pipe(enrichUserData) |
| 75 | + .tap(cacheService.store) // Cache the enriched data |
| 76 | + .pipe(validateUser) |
| 77 | + .tapException(e => alertService.sendAlert(s"User processing failed: $e")) |
| 78 | +} |
| 79 | + |
| 80 | +// Critical operation that shouldn't be interrupted |
| 81 | +def saveToDatabase(data: Data): Unit = uninterruptible { |
| 82 | + database.beginTransaction() |
| 83 | + database.save(data) |
| 84 | + database.commit() |
| 85 | + auditLog.record(s"Saved data: ${data.id}") |
| 86 | +} |
| 87 | + |
| 88 | +// Working with legacy async code |
| 89 | +def integrateLegacyService(): String = { |
| 90 | + val futureData = legacyService.fetchDataAsync() |
| 91 | + val data = futureData.get() // Convert to direct-style |
| 92 | + |
| 93 | + processData(data) |
| 94 | + .tap(result => logger.info(s"Processed: $result")) |
| 95 | + .pipe(_.toString) |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Best Practices |
| 100 | + |
| 101 | +1. **Use `pipe`** for transformation chains |
| 102 | +2. **Use `tap`** for side effects that don't change the value |
| 103 | +3. **Use `debug`** during development for easy debugging |
| 104 | +4. **Use `uninterruptible`** for critical sections that must complete |
| 105 | +5. **Use `.discard`** to explicitly ignore return values |
| 106 | +6. **Use `.get()`** to integrate Future-based APIs into direct-style code |
| 107 | + |
| 108 | +## Common Patterns |
| 109 | + |
| 110 | +```scala |
| 111 | +// Good: Clear data processing pipeline |
| 112 | +data |
| 113 | + .pipe(validate) |
| 114 | + .tap(logValidation) |
| 115 | + .pipe(transform) |
| 116 | + .tap(cache.store) |
| 117 | + .pipe(serialize) |
| 118 | + |
| 119 | +// Good: Critical cleanup with uninterruptible |
| 120 | +uninterruptible { |
| 121 | + resource.close() |
| 122 | + tempFiles.cleanup() |
| 123 | + locks.release() |
| 124 | +} |
| 125 | + |
| 126 | +// Good: Debug complex expressions |
| 127 | +val complexResult = debug { |
| 128 | + data.filter(_.isValid) |
| 129 | + .map(transform) |
| 130 | + .reduce(combine) |
| 131 | +} // Shows both expression and result |
| 132 | +``` |
0 commit comments