Skip to content

Add alt as a more permissive alternative to or#851

Merged
vlovgr merged 6 commits intovlovgr:masterfrom
morgen-peschke:add-recovery-combinator
Jun 13, 2025
Merged

Add alt as a more permissive alternative to or#851
vlovgr merged 6 commits intovlovgr:masterfrom
morgen-peschke:add-recovery-combinator

Conversation

@morgen-peschke
Copy link
Copy Markdown
Contributor

@morgen-peschke morgen-peschke commented Jun 13, 2025

Adds alt as a more permissive alternative to or as a possible solution for #850

These helpers will be assumed for the examples:

val l  = ConfigValue.loaded(ConfigKey("loaded 0", 0))
val l1 = ConfigValue.loaded(ConfigKey("loaded 1", 1))

val mE  = ConfigError.Missing(ConfigKey("missing 0"))
val mE1 = ConfigError.Missing(ConfigKey("missing 0"))
val m  = ConfigValue.failed(mE)
val m1 = ConfigValue.failed(mE0)

val fE  = ConfigError("failed 0")
val f  = ConfigValue.failed(fE)

val d  = ConfigValue.default(2)
val d1 = ConfigValue.default(3)

When a is a singular value, the behavior of or and alt are identical:

lhs rhs lhs.or(rhs) lhs.alt(rhs)
l * Loaded: 0 Loaded: 0
f * Failed with fE Failed with fE
m l Loaded: 0 Loaded: 0
m m1 Failed with mE.or(mE1) Failed with mE.or(mE1)
m f Failed with mE.or(fE) Failed with mE.or(fE)
m d Loaded: 2 Loaded: 2
d l Loaded: 0 Loaded: 0
d m Loaded: 2 Loaded: 2
d f Failed with fE Failed with fE
d d1 Loaded: 2 Loaded: 2

When a is a compound values, the behavior of or and alt diverges in
these specific failure cases (and only in these cases):

lhs rhs lhs.or(rhs) lhs.alt(rhs)
(m, l).parTupled l1 Failed with mE.and(Loaded) Loaded: 1
(m, d).parTupled l Failed with mE Loaded: 0
(l, m).parTupled l1 Failed with Loaded.and(mE) Loaded: 1
(d, m).parTupled l Failed with mE Loaded: 0

@vlovgr vlovgr changed the title Add findValid as a more permissive alternative to or Add alt as a more permissive alternative to or Jun 13, 2025
@vlovgr
Copy link
Copy Markdown
Owner

vlovgr commented Jun 13, 2025

Thanks a lot for this @morgen-peschke! I've done some minor refactorings and renamed findValid to alt (including updating the pull request title and description with the new name). I've also added a section on alternatives to the documentation to describe alt.

Edit: note these two cases:

lhs rhs lhs.or(rhs) lhs.alt(rhs)
(m, d).parTupled l Failed with mE Loaded: 0
(d, m).parTupled l Failed with mE Loaded: 0

do not fail for lhs.or(rhs) since the left-hand side is all missing or defaults.

The remaining two cases are the ones which are different compared to alt.

lhs rhs lhs.or(rhs) lhs.alt(rhs)
(m, l).parTupled l1 Failed with mE.and(Loaded) Loaded: 1
(l, m).parTupled l1 Failed with Loaded.and(mE) Loaded: 1

@vlovgr vlovgr merged commit e0c8767 into vlovgr:master Jun 13, 2025
2 checks passed
@morgen-peschke
Copy link
Copy Markdown
Contributor Author

Thanks for the added polish (especially the docs) :)

@morgen-peschke morgen-peschke deleted the add-recovery-combinator branch June 13, 2025 20:51
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.

2 participants