sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
294 stars 23 forks source link

preserve field ordering when formatting record types #295

Closed davidchambers closed 3 years ago

davidchambers commented 3 years ago

The fields of a record type may be specified in some logical order, so it is a shame to reorder the fields in the type's string representation. This pull request removes the use of Array#sort; a record type's fields are now enumerated in their inherent order rather than in alphabetical order. This also affects the order in which type checking is performed.

If one is defining a record type whose fields are not logically related, one can of course specify the fields in alphabetical order.

If this pull request is merged, equivalent record types may have different string representations despite being equal according to Z.equals. Although this is unfortunate, it is not without precedent:

> Z.equals (0, -0)
true

> show (0)
'0'

> show (-0)
'-0'

Z.equals does not even guarantee the interchangeability of equal values with the same string representation:

> Z.equals ({}, Object.create (null))
true

> 'toString' in {}
true

> 'toString' in Object.create (null)
false