Created
May 12, 2022 00:21
-
-
Save PadraigK/fe143b08034b04e461a4e5de8eaf114a to your computer and use it in GitHub Desktop.
Function signatures with generics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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