X.U.ExtensibleConf: Perform 'add' before modifying in once(M)

This better matches the documentation.

It is still, however, considered bad practice to rely on the order of
these operations. `f` isn't meant to touch any extensible configuration.
If it happens to do so anyway, it no longer loops. :-)
This commit is contained in:
Tomas Janousek 2021-10-17 22:01:26 +01:00
parent e0c7e35b3d
commit 3dbdc51158

View File

@ -117,7 +117,7 @@ once :: forall a l. (Semigroup a, Typeable a)
=> (XConfig l -> XConfig l) -- ^ 'XConfig' modification done only once
-> a -- ^ configuration to add
-> XConfig l -> XConfig l
once f x c = add x $ maybe f (const id) (lookup @a c) c
once f x c = maybe f (const id) (lookup @a c) $ add x c
-- | Config-time: Applicative (monadic) variant of 'once', useful if the
-- 'XConfig' modification needs to do some 'IO' (e.g. create an
@ -126,4 +126,4 @@ onceM :: forall a l m. (Applicative m, Semigroup a, Typeable a)
=> (XConfig l -> m (XConfig l)) -- ^ 'XConfig' modification done only once
-> a -- ^ configuration to add
-> XConfig l -> m (XConfig l)
onceM f x c = add x <$> maybe f (const pure) (lookup @a c) c
onceM f x c = maybe f (const pure) (lookup @a c) $ add x c