[swift-evolution] [Pitch] #dup -- a duplication "macro"(?)
Daryle Walker
darylew at mac.com
Tue Aug 1 09:14:17 CDT 2017
> On Jul 31, 2017, at 10:23 PM, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
>
> Could we do better than simple repetition? What if we define a “$$n” unit, where “n” is a nonnegative integer. For each comma-separated item, the unit will be replaced by increasing compiler-constant integers from zero. If using an expression, maybe you can call a function on the double-dollar value to get your own custom values there. This can be expanded to types once value-based generic parameters get added.
>
> When there are nested #dup calls, the double-dollar unit refers to the counter for the innermost #dup. Maybe there can be a triple-dollar unit for the immediately enclosing #dup. Even quadruple- or more-dollar units for the outer levels. (Hopefully, nothing more that one level would be needed.)
Upon waking up, I realized I got this part of the idea completely wrong. Within the right side of a #dup, there is only one counter, so numbers wouldn’t be necessary after the “$$”.
However, we could use numbers to replace the triple-, quadruple-, etc. dollar-sign idea. So “$$0” is the counter for the current #dup, “$$1” is the counter for the immediately surrounding #dup, and higher numbers go for outer layers. Upon expansion, “$$0” goes to the index number of the comma-separated entity, while “$$n” goes to “$$(n - 1)”. It’s an error to still have double-dollar symbols remaining after the outermost #dup is expanded.
Oh, the right side of the #dup can take a type, an expression, or a comma-separated list of one of those.
Here’s an example using theoretical fixed-size arrays:
let tupleOf12 = ( (2, 3, 5, 7), (4, 6, 8, 9), (10, 11, 12, 1) )
let arrayOf12 = { 3, 4 ; #dup(3; #dup(4; tupleOf12 . $$1 . $$0 ) ) }
assert( tupleOf12.0.0 == arrayOf12[0, 0] ) // 2
//…
assert( tupleOf12.2.3 == arrayOf12[2, 3] ) // 1
So resolving the inner #dup gives:
#dup(3; tupleOf12 . $$0 . 0, tupleOf12 . $$0 . 1, tupleOf12 . $$0 . 2, tupleOf12 . $$0 . 3 )
And doing the remaining #dup gives:
tupleOf12 . 0 . 0, tupleOf12 . 0 . 1, tupleOf12 . 0 . 2, tupleOf12 . 0 . 3, tupleOf12 . 1 . 0, tupleOf12 . 1 . 1, tupleOf12 . 1 . 2, tupleOf12 . 1 . 3, tupleOf12 . 2. 0, tupleOf12 . 2 . 1, tupleOf12 . 2 . 2, tupleOf12 . 2 . 3
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170801/ddd12b60/attachment.html>
More information about the swift-evolution
mailing list