Stored property 'base' of 'Sendable'-conforming struct 'AnyShape' has non-sendable type '(CGRect) -> Path'; this is an error in the Swift 6 language mode

Since I updated my project I'm getting this error

Stored property 'base' of 'Sendable'-conforming struct 'AnyShape' has non-sendable type '(CGRect) -> Path'; this is an error in the Swift 6 language mode

I get this error at that struct, more specifically on the base variable

public struct AnyShape: Shape {
    private var base: (CGRect) -> Path
    
    public init<S: Shape>(shape: S) {
        base = shape.path(in:)
    }
    
    public func path(in rect: CGRect) -> Path {
        base(rect)
    }
}

I have no idea how to solve this issue, I've been looking on the internet for same issues and get nothing yet

Answered by DTS Engineer in 805728022

Hello @bruno_mond,

There are a many different ways that you could address this issue, one alternative might be to store the shape itself (which is Sendable) instead of a reference to a function on the shape (which is not Sendable), for example:

public struct AnyShape: Shape {
    private var base: any Shape
    
    public init<S: Shape>(shape: S) {
        base = shape
    }
    
    public func path(in rect: CGRect) -> Path {
        base.path(in: rect)
    }
}

Having said that, you might want to consider if this "AnyShape" design is necessary, given that it effectively fills the same role as "any Shape".

Best regards,

Greg

Hello @bruno_mond,

There are a many different ways that you could address this issue, one alternative might be to store the shape itself (which is Sendable) instead of a reference to a function on the shape (which is not Sendable), for example:

public struct AnyShape: Shape {
    private var base: any Shape
    
    public init<S: Shape>(shape: S) {
        base = shape
    }
    
    public func path(in rect: CGRect) -> Path {
        base.path(in: rect)
    }
}

Having said that, you might want to consider if this "AnyShape" design is necessary, given that it effectively fills the same role as "any Shape".

Best regards,

Greg

Stored property 'base' of 'Sendable'-conforming struct 'AnyShape' has non-sendable type '(CGRect) -&gt; Path'; this is an error in the Swift 6 language mode
 
 
Q