This has come up several times on the forums over the years, but I’ve never written it up in a standard place, so here it is: There are only three ways to get run-time polymorphism in Swift. Well, three and a half.
Default Arguments and Label-based Overloading
This post is in response to Aria Beingessner’s “Defaults Affect Inference in Rust: Expressions Instead Of Types”, which describes how adding default arguments to Rust could help with some Rust stdlib problems around generics. At the same time, the Rust internals forum has a thread on “Named Arguments” something that’s been discussed for Rust off and on for years (at varying levels of seriousness).
Here I’m going to discuss how those two features interact, and why considering them separately is potentially a bad idea. It’s a lot more braindump-y than my usual style, so be warned. The post is written with a Rust audience in mind, but makes frequent reference to Swift, Python, and C# as examples of real-world languages that have some version of these features. It also overlaps quite a bit with Aria’s post.
Swift Regrets: Wrap-up
Thus ends five months of Swift regrets (and delights), things that I had been collecting during my last year at Apple…and things that came up during the ensuing discussion on Twitter. I wanted to talk about these things because every project learns from those that came before it, and that should include both the good parts and the bad parts. I have a memory of my former colleague Joe Groff saying that we should normalize and encourage talking about mistakes and missteps in our field, and so this is a contribution.
Older Posts
- 2021-09-10 Swift Regrets
- 2018-02-26 Many-to-Many Protocols
- 2017-09-07 The New Kingdom of Nouns
- 2015-12-29 Re: Contempt Culture
- 2015-11-18 Recommendations
- 2011-06-16 Dealing with "Sandwich Code"
- 2009-06-26 Garbage Collectors and Stack Drawers
- 2009-05-05 C++ Templates are Turing-Complete
- 2009-03-27 Const Correctness
- 2008-09-04 Objective-J and Objective-C