blammo icon indicating copy to clipboard operation
blammo copied to clipboard

HasLogger wants a Lens, but only needs a Getter?

Open sjoerdvisscher opened this issue 11 months ago • 4 comments

Blammo's HasLogger requires loggerL :: Lens' env Logger, which implies that it can not only get a Logger from env but that it can also modify the Logger in env. But looking at the source code it seems only view is ever used on loggerL.

So maybe it would be a good idea to change loggerL to loggerL :: Getter env Logger? This is a backwards-compatible change if I'm not mistaken.

sjoerdvisscher avatar Dec 22 '25 15:12 sjoerdvisscher

We only use view within the library itself, but we allow modifications through the lens for users of our library to do things we may not have thought of. For example, changing the logger (or something about it) for a block of code under test, using local / over, %~, etc.

Is there a reason restricting it to the less powerful thing is beneficial?

pbrisbin avatar Dec 22 '25 15:12 pbrisbin

That sounds reasonable.

I'm in a situation where having to provide the full lens adds an extra constraint that is annoying to work with.

sjoerdvisscher avatar Dec 22 '25 15:12 sjoerdvisscher

I see. If you don't plan to use the setting behavior, I think it'd be fine to supply a no-op, to avoid any additional constraints:

instance HasLogger env where
  loggerL = lens yourGetter $ \x _ -> x

Right?

pbrisbin avatar Dec 22 '25 15:12 pbrisbin

Yeah, although then there's no guarantee somebody else adds code to our codebase that tries to modify the logger.

What I ended up doing is writing custom MonadLogger(IO) instances for our App monad that doesn't use the HasLogger class.

sjoerdvisscher avatar Dec 22 '25 16:12 sjoerdvisscher