[swift-evolution] [Discussion] Here we go again: Extension Functions

Stephen Celis stephen.celis at gmail.com
Mon Feb 29 18:04:33 CST 2016


> On Feb 29, 2016, at 6:38 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
> 
> Personally, I wouldn't mind having the feature structured in the following fashion:
> 
> * You can use `self` as the internal name of any parameter to any function. (Though perhaps not in a method, which already has an implicit parameter with the internal name `self`.)
> * If you do, that parameter is the receiver of all receiverless method calls.
> * A closure with `self: T` in its parameter list is simply a hint to SourceKit that its code completion should name the parameter `self`.
> 
> That way, any closure which rebound self would be explicitly marked at the top with `self in`, and if you wanted to access the outer `self`, you could always simply give the parameter a different name.
> 
> This would add a small syntactic burden to uses of `with` and other DSLs, but it might be worth it to make the behavior explicit.

The `self in` solution was actually one of the first suggestions on this list:

https://lists.swift.org/pipermail/swift-evolution/2015-December/000114.html

I do think it gets rather wordy in DSLs, though, and that implicit `self` should be favored if it works (for the same reason we allow implicit `self`). There's a lot of noise:

    html { self in
        head { self in
            title("Hello")
        }
        body { self in
            section { self in
                p("Hello, ", span(name))
            }
        }
    }

Vs.

    html {
        head {
            title("Hello")
        }
        body {
            section {
                p("Hello, ", span(name))
            }
        }
    }

Even "$0" feels less noisy, depending on the DSL:

    html {
        $0.head {
            $0.title("Hello")
        }
        $0.body {
            $0.section {
                $0.p("Hello, ", $0.span(name))
            }
        }
    }

--
Stephen



More information about the swift-evolution mailing list