Skip to content

Add when and unless for conditional value resolution#264

Open
phanan wants to merge 1 commit intomainfrom
feature/when-unless
Open

Add when and unless for conditional value resolution#264
phanan wants to merge 1 commit intomainfrom
feature/when-unless

Conversation

@phanan
Copy link
Collaborator

@phanan phanan commented Mar 21, 2026

Summary

  • Adds when($condition, $value, $default = null) — returns $value if $condition is truthy, otherwise $default
  • Adds unless($condition, $value, $default = null) — returns $value if $condition is falsy, otherwise $default
  • All three arguments can be closures: $condition is evaluated, and $value/$default are resolved accordingly
use function Functional\when;
use function Functional\unless;

when(true, 'yes');              // 'yes'
when(false, 'yes', 'no');      // 'no'
when(fn () => true, 'yes');    // 'yes'
when(true, fn () => 'computed'); // 'computed'

unless(false, 'yes');           // 'yes'
unless(true, 'yes', 'no');     // 'no'

Test plan

  • Unit tests for when — truthy/falsy conditions, closures for condition/value/default (7 tests)
  • Unit tests for unless — truthy/falsy conditions, closures for condition/value/default (7 tests)

@phanan phanan force-pushed the feature/when-unless branch 2 times, most recently from cd0b18e to 3e2d2d1 Compare March 21, 2026 19:09
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@phanan phanan force-pushed the feature/when-unless branch from 3e2d2d1 to d6aa473 Compare March 21, 2026 19:34
@phanan phanan changed the title Add when and unless for single-branch conditional transforms Add when and unless for conditional value resolution Mar 21, 2026
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.

1 participant