Skip to content

Instantly share code, notes, and snippets.

@PadraigK
Created May 12, 2022 00:21
Show Gist options
  • Save PadraigK/fe143b08034b04e461a4e5de8eaf114a to your computer and use it in GitHub Desktop.
Save PadraigK/fe143b08034b04e461a4e5de8eaf114a to your computer and use it in GitHub Desktop.
Function signatures with generics
func makeOrReuse<T : IdentifierCellView>(_ cellType: T.Type, tableView: NSTableView) -> T
func makeOrReuse<T : IdentifierCellView>(tableView: NSTableView) -> T
// IMHO: The first signature is vastly superior even though the first argument is unncessary.
// Think about the call sites
let cell = makeOrReuse(ImageExportOptionCells.Toggle.self, tableView: tableView)
// vs
let cell : ImageExportOptionCells.Toggle = makeOrReuse(tableView: tableView)
// With the second option, the type definition of the receiving variable is effectively an additional argument to the
// method, but it doesn't _read_ like that at all.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment