Repository navigation
Add variables, loop positions, ? :, whitespace trimming, byte output and template objects - #14
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Before: templates had no way to name a computed value, tell the first or last loop element apart, choose between two values inline, or trim whitespace next to a tag. Servers had to turn rendered Strings back into bytes, and every context had to be converted into
TemplateValues up front.After:
{% set total = order.items.@sum.price %}stores a variable. Setting one that already exists changes it, so a loop can add to a total set before the loop. A variable first set inside a loop goes out of scope when the loop ends. One set inside anifstays in scope after it.itemIsFirstanditemIsLastalongsideitemIndex.condition ? then : otherwiseworks anywhere an expression does, and only the side the condition picks is evaluated, in strict mode too.{%-and-%}trim the whitespace before or after a tag, line breaks included.render(_:features:bytes:)hands over UTF-8 bytes in chunks and passes errors from the write closure on to the caller.TemplateObjectlets a value give the template one key at a time, asTemplateValue.object(_:). On Apple platforms,TemplateValue(any:)andrender(object:)read otherNSObjects, such as Core Data objects, the same way through their@objcproperties. Before, these rendered only as their description.TemplateValuegains a case, so code that switches over it exhaustively needs a.objectbranch. Its==andhash(into:)are now written out by hand with the same meaning as before.How:
setcompiles to a newsetinstruction that writes a variable slot. Slots are no longer reused, so a variable not yet set reads as nil. Loops take four slots instead of two. The conditional is a new syntax node and operation. Value operations read an object's keys throughvalue(forKey:), and use itstemplateValuewherever the whole value is needed.I also tried having
render(object:)read dictionaries lazily. On the list benchmark it was about 1.7x slower, even with a per-key cache, sorender(object:)still converts dictionaries up front. The benchmarks now include arender(object:)row, and rendering speed is the same asmainwithin noise.The new tests pass on Linux with Swift 6.0.3 (174 tests). The KVC test only runs on macOS, so CI is the first place it runs.
Generated by Claude Code