elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.65k stars 299 forks source link

`pprint` (and `repr`) now sorts map keys #1696

Closed krader1961 closed 1 year ago

krader1961 commented 1 year ago

This change is more invasive than I expected because the existing Elvish value comparison code was in pkg/eval rather than pkg/eval/vals. This moves the value comparison logic into the vals package so it can be used by packges other than eval. For example, the vals package repr implementation. Not to mention other Elvish modules which may want to compare Elvish values.

I also decided to introduce a strutil.Dedent function to make it easier to write multiline string literals in unit tests to improve readability of the expected value.

This also sorts the keys of pseudo (struct) maps for consistency with mutable maps. This arguably makes the output of pretty-printing pseudo maps less friendly since a struct map definition typically orders its members in a deliberate fashion (unlike a regular map). However, it is more important that the output of printing the output of regular and pseudo maps (via repr or pprint) be consistent with regard to the order of their keys.

Related: 1495

krader1961 commented 1 year ago

Closing in light of my recent thoughts regarding a total ordering of all Elvish value types (see https://github.com/elves/elvish/issues/1599). I'm going to rework these two changes to make the ordering of different types well defined. Thus making the implicit sorting of map keys by pprint and repr well defined rather than ill-defined.