Skip to content

No backward compatibility for types conforming to both RawRepresentable and Codable #184

@junpluse

Description

@junpluse

Description

Since version 2.7.0, the handling of types in AppStorageKey that conform to both RawRepresentable and Codable has changed. These types are now processed as Codable, which prevents access to values saved in previous versions. This affects types such as the Tagged struct from swift-tagged.

import Sharing
import Tagged

enum Tag {}
typealias TaggedValue = Tagged<Tag, Int>

extension SharedKey where Self == AppStorageKey<TaggedValue>.Default {
    static var value: Self {
        // In swift-sharing 2.6.0, access is done through CastableLookup, 
        // whereas in 2.7.0 it is done through CodableLookup, 
        // resulting in a different data format in UserDefaults.
        Self[.appStorage("value"), default: 0]
    }
}

I understand this change is due to the addition of Codable support in #182, but if this change in behavior is intentional, it may be a good idea to warn library users about it.

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

If the Value of an AppStorageKey conforms to both RawRepresentable and Codable, it can continue to access values saved up to version 2.6.0 even in version 2.7.0 and later.

Actual behavior

If the Value of an AppStorageKey conforms to both RawRepresentable and Codable, it cannot access values saved up to version 2.6.0 in version 2.7.0 or later.

Reproducing project

SharingRawRepresentableCodableDemo.zip

Sharing version information

2.7.0

Destination operating system

iOS 18, macOS 15.6

Xcode version information

16.4

Swift Compiler version information

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions