SafeGlobal

0.3.0

Frizlab/SafeGlobal

What's New

SafeGlobal 0.3

2024-03-14T12:00:37Z

SafeGlobal

A macro to have concurrency-safe globals (e.g. a static variable on an enum) in Swift 5.10.

Usage

Add this package in your Package.swift file and trust it.

Then import SafeGlobal and prefix your global variables with @SafeGlobal. Here’s an example:

import SafeGlobal

public enum Conf : Sendable {
   
   @SafeGlobal public static var apiURL: URL = URL(string: "")!
   
}

Why?

With Swift 5.9, globals were deemed (rightly) non-concurrent-safe. A common workaround was to create a property wrapper which added a lock around the access/modification of the global.

This workaround was great and easy to implement, but does not work anymore with Swift 5.10. The reason for this is related to the property wrapper implementation, but I did not grasp the intricacy of the reasons fully, so I’m not gonna elaborate on this.

Anyway, by basically reimplementing the property wrapper using a macro we get rid of the warnings once again!

Here are the relevant threads in the Swift forum:

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Tue Apr 30 2024 18:06:23 GMT-0900 (Hawaii-Aleutian Daylight Time)