stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 221 forks source link

Deterministic `for` loops for dictionaries. #240

Closed djbe closed 5 years ago

djbe commented 5 years ago

⚠️ Note: based on #239, so merge that first!

With Swift 4.2, hashing isn't consistent between runs anymore. A consequence of this is, if we iterate over a dictionary, the output won't always be the same, which will be unexpected for a user.

With this PR we iterate over the sorted keys of a dictionary, ensuring consistent output across runs.

ilyapuchka commented 5 years ago

I think it's fine though I don't think this was mentioned anywhere in docs (I mean Stencil) that on dictionaries the order sill be deterministic. But this will preserve current behaviour at least.

djbe commented 5 years ago

I think end users would expect consistent behaviour, no matter what the underlying (Swift) behaviour might be?

djbe commented 5 years ago

And with the new swift hashing, we're forced to do something ourselves in this case.

ilyapuchka commented 5 years ago

🤷‍♂️ depends on user understanding of nature of such types =) for some deterministic order in swift < 4.2 could be a surprise too =)

djbe commented 5 years ago

@ilyapuchka I inadvertently merged this into the wrong branch 🤦‍♂️, so we should continue our discussion in #242.