Skip to content

render :: Doc ann -> String considered harmful #70

@andreasabel

Description

@andreasabel

These functions just throw away the annotations:

-- | Render the @Doc@ to a String using the default @Style@ (see 'style').
render :: Doc a -> String
render = fullRender (mode style) (lineLength style) (ribbonsPerLine style)
txtPrinter ""
-- | Render the @Doc@ to a String using the given @Style@.
renderStyle :: Style -> Doc a -> String
renderStyle s = fullRender (mode s) (lineLength s) (ribbonsPerLine s)
txtPrinter ""

Their type signatures are too general, one can shoot oneself in the foot (esp. when using AI for coding).
Their types should be limited to Doc Void, making sure there are no annotations in these documents in the first place. So before their use, the user has to consciously convert Doc ann -> Doc Void to be aware of throwing away annotations.
Or these functions should not be there in the first place, so that the user has to convert to a standard unannotated Doc first and then render this.

Either would be a breaking change so I don't know how practical that is.
Also, I don't know whether a zero-cost implementation

removeAnn :: Doc ann -> Doc Void
removeAnn= coerce

is safe.

The alternative to Void would be () which is safe (use void) but not entirely correct, because in Doc () one can still have annotations (albeit only a single kind of annotation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions