[swift-evolution] Idea for enabling DSLs: bind to self in closures

Kevin Ballard kevin at sb.org
Fri Dec 4 16:51:47 CST 2015

On Fri, Dec 4, 2015, at 09:36 AM, Joe Groff wrote:
> Another way to do this would be to support scoped imports, to make a set of top-level functions locally available without polluting the global namespace:
> {  import func QuickSpecBuilder.expect
> expect(sections).to(....) }
> Being able to elide self is already somewhat controversial, and a
> number of people find it makes code harder to read. I worry that
> allowing closures to change 'self' has the potential to be even more
> confusing. In Javascript, it's my understanding the ability to
> arbitrarily rebind 'this' is seen as a design flaw rather than a
> feature people regularly take advantage of.

I like this idea. I'm very much against rebinding `self` because it
seems like an excellent source for confusion. Not only that, but the
actual underlying desire here isn't to remove `self` at all, but just to
introduce new functions into function resolution within a scope. And
this is precisely what adding imports in arbitrary scopes does (the only
downside being you need a line of code to add them, but that's not a big
deal). I know Rust allows this and it's pretty handy. I'd love to have
this feature even when not using a DSL.

-Kevin Ballard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/506870b4/attachment.html>

More information about the swift-evolution mailing list