Skip to content

Add static analyzer warnings for lazy evaluation in monad functors - #54

Draft
MrBogomips with Copilot wants to merge 2 commits into
rel/prodfrom
copilot/fix-4
Draft

MrBogomips with Copilot wants to merge 2 commits into
rel/prodfrom
copilot/fix-4

Conversation

Copilot AI commented Jul 2, 2025

Copy link
Copy Markdown
Contributor

This PR implements static analyzer warnings to help developers leverage lazy evaluation benefits when using monad functors. The warnings appear when users pass direct values or function calls to methods that have lazy alternatives, potentially missing out on performance optimizations.

Problem

Many functor methods accept both direct values and functions. When users pass function calls directly instead of lambdas, they lose lazy evaluation benefits:

var maybe = Maybe.None<string>();

// BAD: ExpensiveMethod() executes even though maybe is None
var result = maybe.Map(ExpensiveMethod()); // Always calls method

// GOOD: ExpensiveMethod() only executes if maybe has a value  
var result = maybe.Map(() => ExpensiveMethod()); // Lazy evaluation

Solution

Added [Obsolete] attributes with warning level (not error) to methods that accept direct values when lazy alternatives exist:

Maybe Methods

  • Map(TValue value) → warns to use Map(() => value)
  • WithDefault(TValue value) → warns to use WithDefault(() => value)

Result Methods

  • RecoverWith(TValue value) → warns to use RecoverWith(() => value)

Async Variants

  • Task<Maybe<T>>.WithDefault(TValue value)
  • Task<Result<T>>.RecoverWith(TValue value)

Performance Impact

Demonstration of the performance difference:

var maybe = Maybe.None<string>();

// Eager evaluation: method called unnecessarily (1 call)
var eager = maybe.Map(ExpensiveMethod());

// Lazy evaluation: method not called (0 calls)  
var lazy = maybe.Map(() => ExpensiveMethod());

Features

  • Backward Compatible: Existing code continues to work, just generates compile-time warnings
  • Clear Guidance: Warning messages explain the lazy evaluation benefits
  • Comprehensive Coverage: Covers all major functor methods across Maybe and Result
  • Documentation: Added detailed guide explaining when and how to use each approach
  • Suppressible: Warnings can be suppressed with #pragma warning disable CS0618 when intentional

Testing

  • Added 9 comprehensive tests demonstrating warnings and lazy evaluation benefits
  • All existing tests continue to pass (336 total tests)
  • Created example programs showing performance differences
  • Verified warnings appear correctly during compilation

Documentation

Added comprehensive documentation at docs/STATIC_ANALYZER_WARNINGS.md covering:

  • Why lazy evaluation matters
  • Migration guide for existing code
  • Performance impact examples
  • When to use each approach

Fixes #4.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: MrBogomips <864417+MrBogomips@users.noreply.github.com>
Copilot AI changed the title [WIP] Static Analyzers Add static analyzer warnings for lazy evaluation in monad functors Jul 2, 2025
Copilot AI requested a review from MrBogomips July 2, 2025 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Static Analyzers

2 participants