# Pitch: Scoped Functions

**URL:** <https://forums.swift.org/t/pitch-scoped-functions/45486>\
**Category:** Pitches\
**Created:** [March 9, 2021, 7:33pm UTC](https://forums.swift.org/t/pitch-scoped-functions/45486 "2021-03-09T19:33:25Z")\
**Posts on this page:** 1\
**Showing post:** 46

<div class="post-metadata">

**Author:** ![iabudiab](https://avatars.discourse-cdn.com/v4/letter/i/f0a364/32.png) [@iabudiab](https://forums.swift.org/u/iabudiab)\
**Post date:** [March 16, 2021, 8:22pm UTC](https://forums.swift.org/t/pitch-scoped-functions/45486/46 "2021-03-16T20:22:19Z")

</div>

There was a very similar pitch back in 2019 [Receiver Closures](https://forums.swift.org/t/receiver-closures/24020), which was also inspired by another Kotlin feature: [Function literals with receiver](https://kotlinlang.org/docs/lambdas.html#function-literals-with-receiver)  
And I do hope that either one of these pitches will be considered and eventually implemented.

Perhaps the main point to address here is this:

> [@tevelee](#):
>
> This proposal is centered around the syntax of how scoped function could look like, but it doesn’t go into why they should exist in the first place. I would rather take the angle of defining it first, have a design centered around the definition and syntax will naturally follow.

My 2 cents here. Many (real-world) concepts and domains can be elegantly abstracted with a DSL. Having worked with Kotlin I've contributed to some DSLs that were really pleasant to use, which also could be understood by programmers and non-programmes alike, because of the familiarity with the domain.

This feature would be a niche, as it is in Kotlin, however it would allow library authors to provide very very convenient and elegant APIs.

Instead of the usual HTML example, I would like to show the API I'd like to provide for [SwiftkubeModel](https://github.com/swiftkube/model) for building Kubernetes API objects programmatically:

> This just a simple example, check out this [k8s-kotlin-dsl repository](https://github.com/fkorotkov/k8s-kotlin-dsl) for a complete K8s DSL built with Kotlin using function receivers:

```
let nginx = deployment {
  metadata {
    name = "nginx"
    labels = ["app": "nginx"]
  }
  spec {
    replicas = 2
    selector {
      matchLabels(["app": "nginx"])
    }
    template {
      spec {
        containers {
          conrainer("nginx") {
            image = "nginx:latest"
          }
        }
      }
    }
  }
}

```

it is readable, type-safe, auto-completable, and **above all** it is **familiar** for everyone working with K8s.

---

_[View the full topic](https://forums.swift.org/t/pitch-scoped-functions/45486)._
