[Pitch] Usability of global-actor-isolated types

Hello,

Thanks for those QoL improvements!

I was expecting to see something about global-actor isolated type that define public static properties that have a Sendable types. Those are a real pain point. See https://forums.swift.org/t/difficulty-designing-a-static-requirement-due-to-sendable-se-0412/70107/29:

ADDENDUM:

I can't put my finger on it now (some Mastodon thread), but it looks like there is two distinct problems with static properties of Sendable type:

@MainActor
public class MyType {
    // 1
    public static let constant = "foo"

    // 2
    public static var notConstant: String { "bar" }
}

I was referring to 1, the constant, in the beginning of this post. It should be nonisolated:

@MainActor
public class MyType {
    // The CORRECT (i.e. not painful for users) declaration
    public nonisolated static let constant = "foo"
}

On this other side, the var notConstant, as declared, is isolated, and should be, since it is declared as mutable.

But some Objective-C frameworks define constants that are imported as var. That's where I can't find the link to a recent example.

I forgot my Objc, so please excuse this gross attempt at reproducing the problem:

// That's a static property exported as `var` in Swift, right?
NS_SWIFT_UI_ACTOR
@interface MyType
@property (class, readonly) NSString *constantButExportedAsVar;
@end

@implementation MyType
@dynamic constantButExportedAsVar;
+ (NSString *)constantButExportedAsVar {
    // This is constant, but nobody knows 😖
    // Swift compiler can't suggest to make it nonisolated.
    return @"bar";
}
@end

I would expect some audit of Apple frameworks in this regard.

4 Likes