Why Actor in Swift Don't Support Inheritance

Nurul IslamNurul Islam
2 min read

In the world of concurrent programming, Swift's actors provide a powerful abstraction for ensuring thread-safety and avoiding common concurrency pitfalls like data races. However, one design decision that might surprise some developers is the lack of inheritance support for actors in Swift. This intentional choice by the Swift team aims to preserve the safety guarantees and isolation properties that make actors so effective.

Inheritance, a familiar concept from object-oriented programming, allows subclasses to inherit properties and behaviors from their superclasses. While inheritance can promote code reuse and extensibility, it can also introduce potential concurrency issues when applied to actors. If an actor could inherit from another actor, there would be a risk of sharing mutable state between them, leading to potential data races and other concurrency issues that violate the actor's isolation and mutual exclusion guarantees.

To address this concern while still enabling code reuse and shared behavior, the Swift team has provided alternative mechanisms:

  1. Actor protocol: Actors inherit from the Actor protocol, gaining built-in concurrency features and isolation guarantees. This approach promotes separation of concerns and keeps actor behavior focused on safe concurrent access.

  2. Protocols: Actors can conform to protocols, defining shared behaviors and functionalities with other types without inheriting implementation details.

  3. Composition: Instead of inheritance, you can combine multiple actors with different functionalities to achieve desired behavior, promoting a more modular and composable design.

  4. Extensions: Extensions can add new methods and properties to existing actors, similar to subclassing in classical object-oriented programming, but without inheriting mutable state.

By disallowing inheritance for actors, the Swift team prioritizes safety and correctness in concurrent programming models, ensuring that actors maintain their isolation boundaries and mutual exclusion guarantees. While this decision may require developers to adjust their thinking and embrace alternative approaches, it ultimately contributes to the robustness and reliability of concurrent systems built with Swift.

As concurrent programming becomes increasingly prevalent, design decisions like this underscore Swift's commitment to providing safe and effective abstractions for managing concurrency challenges. By understanding the reasoning behind such choices, developers can better leverage the power of actors and other concurrency models in Swift while adhering to best practices for thread-safety and avoiding common concurrency pitfalls.

2
Subscribe to my newsletter

Read articles from Nurul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nurul Islam
Nurul Islam

I'm an experienced mobile application developer with over 10 years of expertise in iOS development using Swift, Objective-C, and Flutter. Throughout my career, I've had the opportunity to work for multiple renowned companies where I developed and architected robust mobile applications used by millions of users worldwide. In addition to my extensive iOS experience, I also have experience working with ASP.Net Core, Entity Framework, and LINQ for backend development as well as Machine Learning and Deep Learning using Python and TensorFlow which has allowed me to collaborate closely with different teams to develop different types of products, apps or services.