[swift-evolution] Object declarations
Marc Knaup
marc at knaup.koeln
Fri Dec 11 16:07:03 CST 2015
Hey guys,
I am working on a proposal which touches (and could even partially depend
on) another interesting concept which I think would be a great addition for
Swift.
Object declarations could be a way to declare a class or struct with
exactly one instance. They are perfect for singletons, anonymous subclasses
and to anonymously implement a protocol.
Here some examples how it could look like:
// singleton
object GlobalResource {
func doSomething() { … }
}
let resource = GlobalResource
// could all be the same
resource.doSomething()
GlobalResource.doSomething()
GlobalResource.Type.doSomething()
// anonymous subclass
class ViewController: UIViewController {
override func loadView() {
view = object: UIView {
// configure view
}
}
}
// anonymous protocol implementation
protocol TapHandler {
func handleTap()
}
view.tapHandler = object: TapHandler {
func handleTap() {
// …
}
}
Kotlin is an example for a a modern language which uses this concept:
https://kotlinlang.org/docs/reference/object-declarations.html
What do you think about such an addition?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/548041e7/attachment.html>
More information about the swift-evolution
mailing list