Wrong specialized generic function gets called in Swift 3 from an indirect call

This is indeed correct behaviour as overload resolution takes place at compile time (it would be a pretty expensive operation to take place at runtime). Therefore from within test(value:), the only thing the compiler knows about value is that it’s of some type that conforms to DispatchType – thus the only overload it can dispatch … Read more

Is there a way to store a function in a list or dictionary so that when the index (or key) is called it fires off the stored function?

Functions are first class objects in Python and so you can dispatch using a dictionary. For example, if foo and bar are functions, and dispatcher is a dictionary like so. dispatcher = {‘foo’: foo, ‘bar’: bar} Note that the values are foo and bar which are the function objects, and NOT foo() and bar(). To … Read more

Using a dispatch_once singleton model in Swift

tl;dr: Use the class constant approach if you are using Swift 1.2 or above and the nested struct approach if you need to support earlier versions. From my experience with Swift there are three approaches to implement the Singleton pattern that support lazy initialization and thread safety. Class constant class Singleton { static let sharedInstance … Read more