Skip to content

Add functions traceWith, traceShowWith to Debug.Trace #36

@ChickenProp

Description

@ChickenProp

(Historical note: originally this used On instead of With, but consensus seems to prefer With.)

These could be implemented simply as

traceWith :: (a -> String) -> a -> a
traceWith f a = trace (f a) a

traceShowWith :: Show b => (a -> b) -> a -> a
traceShowWith f a = traceShow (f a) a

I'd frequently find these functions useful when debugging, to give me just a small part of the large data structure being produced. But since I don't keep Debug.Trace imported long-term, I'm not going to have these defined as helper functions anywhere, so I basically just end up writing the definition inline - where I want traceShowWith f a I'd write (\x -> traceShow (f x) x) a which is awkward and obscures the actual code I'm trying to debug.

(I think flip traceShow <*> f is the same, but I wouldn't be confident I've remembered it correctly - and when I'm debugging, the last thing I want is to be wondering whether my debugging functions are doing what they should.)

This is admittedly only a mild quality-of-life improvement. But I think it's low cost. There's potential breakage because it adds exports, but I'd strongly expect there to be little-or-none in practice, and it's easy to fix where it does show up.

ETA: @noughtmare points out we'd also want to do this for traceEvent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedApproved by CLC votebase-4.18Implemented in base-4.18 (GHC 9.6)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions