Argument labels for closures were dropped in SE-0111. In the
"additional commentary" attached to SE-0111 on its acceptance, there was
a thought that the labels would be added back to declarations. Seems
like that hasn't happened, yet. Is it on the roadmap?
I'm motivated to think about this, today, because I'd like to grant
parts of a program access to a subset of the public methods on a class
instance, and one obvious way to do that is to bundle closures for those
methods into structs. For example, this is a sketch of code to bundle
methods that modify a Rope into struct Writer, and methods that only
read a Rope into struct Reader:
class Rope {
public func copy(_ r: Range<Index>) -> [PasteAction] {
// ...
}
public func paste(_ actions: [PasteAction],
over r: Range<Index>, reversals changes: ChangeList<Rope>) throws {
// ...
}
public func replace(_ r: Range<Index>, with replacement: Content,
reversals changes: ChangeList<Rope>) throws {
// ...
}
struct Reader {
let copy: (_: Range<Index>) -> [PasteAction]
init(for rope: Rope) {
self.copy = rope.copy
}
}
struct Writer {
let paste: (_: [PasteAction], over: Range<Index>,
reversals: ChangeList<Rope>) throws
let replace(_: Range<Index>, with: Content,
reversals: ChangeList<Rope>) throws
init(for rope: Rope) {
self.paste = rope.paste
self.replace = rope.replace
}
}
}
let c = Rope()
let reader = Rope.Reader(for: rope)
let writer = Rope.Writer(for: rope)
// Note that in a realistic example, `reader` and `writer` would be
// delegated to different parts of the program that operated
// having different responsibilities and/or level of trust
let actions = reader.copy(copyRange)
writer.paste(actions, over: pasteRange, reversals: ...)
Today, unfortunately, the function-typed members of structs Reader
and Writer cannot be called with argument labels. I can accomplish
what I want by writing short methods on the structs that forward their
arguments to a private Rope instance, but that's a lot of boilerplate
code to write. Hence the desirability of using argument labels with
closures.
If somebody can suggest a way to accomplish a similar division of access
without writing a bunch of boilerplate, I'd like to hear about it.
Dave