Explicit Discard (discard)
Explicit Discard (discard)
Section titled “Explicit Discard (discard)”Profile: :core and above
Purpose: Explicitly discard the result of an expression
Motivation: Clear, readable code that signals intent to ignore return values
The Problem
Section titled “The Problem”When calling functions for their side effects, you often don’t care about the return value. In many languages, this is implicit and potentially dangerous:
// C: Return value silently ignoredwrite(fd, buffer, size); // Did it succeed? Who knows!In Zig, you use _ = expr to explicitly discard:
// Zig: Explicit but cryptic_ = posix.close(fd);_ = allocator.free(buf);The _ syntax is concise but not self-documenting. Newcomers often don’t understand what it means.
The Janus Solution
Section titled “The Janus Solution”Janus uses the discard keyword for explicit clarity:
// Janus: Explicit and readablediscard posix.close(fd)discard allocator.free(buf)The word “discard” makes the intent obvious: “I am intentionally ignoring this result.”
Syntax
Section titled “Syntax”discard expressionThe discard keyword evaluates the expression and discards its result. This is useful for:
- Side-effect only functions
- Fire-and-forget async tasks
- Logging and debugging output
Examples
Section titled “Examples”Discarding Return Values
Section titled “Discarding Return Values”// Close a file descriptor, ignore return codediscard posix.close(fd)
// Free memory, ignore success/failurediscard allocator.free(buf)
// Write log message, ignore bytes writtendiscard logger.write("Application started")With Error Handling
Section titled “With Error Handling”You can combine discard with error handling to ignore success values but handle errors:
discard may_fail() catch |err| do log_error("Operation failed:", err)endIn Loops
Section titled “In Loops”// Process items, discard intermediate resultsfor items do |item| discard process_item(item)endMultiple Discards
Section titled “Multiple Discards”// Cleanup sequencediscard posix.close(fd)discard allocator.free(buf)discard logger.write("Cleanup complete")Comparison with Other Languages
Section titled “Comparison with Other Languages”| Language | Discard Syntax | Notes |
|---|---|---|
| Janus | discard expr | Explicit, readable |
| Zig | _ = expr | Cryptic to newcomers |
| Rust | let _ = expr | Pattern discard, verbose |
| Go | _ = expr | Same as Zig |
| C | expr | Silent, dangerous |
Relationship to _
Section titled “Relationship to _”Janus still uses _ for pattern-level discards:
// Pattern discard: ignoring a value in destructuringlet (x, _, z) = tuple // Ignore middle element
// Catch discard: ignoring the error valueresult catch |_| do // Handle any error uniformlyend
// For loop discard: ignoring the indexfor _ in 0..10 do println("Hello")endUse discard for statement-level result discarding, _ for pattern-level binding discarding.
Best Practices
Section titled “Best Practices”✅ DO Use discard When:
Section titled “✅ DO Use discard When:”- Calling functions for side effects only
- Fire-and-forget async operations
- Cleanup operations where you don’t care about success/failure
- Making intent explicit to readers
// Good: Clear intentdiscard logger.info("User logged in")❌ DON’T Use discard When:
Section titled “❌ DON’T Use discard When:”- You should handle the error (use
tryorcatch) - The return value contains important information
- You’re in a pattern context (use
_instead)
// Bad: Silently ignoring errorsdiscard risky_operation() // Don't do this!
// Good: Handle errors explicitlytry risky_operation() catch |err| do handle_error(err)endRationale
Section titled “Rationale”The discard keyword embodies Janus’s philosophy of Syntactic Honesty:
“Code should say what it means.
_ =says ‘assign to underscore’, which is implementation detail.discardsays ‘I am discarding this result’, which is intent.”
For the :core teaching profile, discard is especially valuable:
- No cryptic symbols to memorize
- Self-documenting code
- Clear distinction from pattern discards (
_)
See Also
Section titled “See Also”- Error Handling - Proper error handling patterns
- Language Reference - Complete syntax guide
- :core Profile - Teaching language profile