Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12255] Mirror behaviour is unspecified and inconsistent for enums with zero-size or no associated values #54682

Closed
swift-ci opened this issue Feb 22, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12255
Radar None
Original Reporter SAJim (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 11.3.1, running in a Swift Playground. (Not sure if this runs Swift 5.1 / 5.2)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 31b5e4ee726e4a8090abd3eff30a75e5

Issue Description:

My expected behaviour, based on the existing results for non-zero-size associated values, would be that Mirror maps cases with associated values to a tuple of the case name and any associated values. If the enum has no associated values, it's not obvious what the result should be, but I'd most expect a plain String case name.

The ideal behaviour, on the other hand, is subject to interpretation of how we should compromise with SE-0155 per the decision not to fully implement it. Under this proposal as written, I would have expected labels to be available as Strings in the reflected representation.

The current state of affairs is illustrated by this code sample:

struct NonEmpty {
    let value = ""
}
struct Empty {}

enum MyEnum {
    case nonEmpty(NonEmpty)
    case duplicateNonEmpty(NonEmpty, s: NonEmpty)
    case empty(Empty)
    case duplicateEmpty(Empty, s: Empty)
    case paddedEmpty(Empty, NonEmpty)
    case noAssociatedValue
}

Mirror(reflecting: MyEnum.nonEmpty(NonEmpty())).children.first
// (label: "nonEmpty", value: NonEmpty(""))

Mirror(reflecting: MyEnum.duplicateNonEmpty(NonEmpty(), s: NonEmpty())).children.first
// (label: "duplicateNonEmpty", (NonEmpty(""), s: NonEmpty("")))

Mirror(reflecting: MyEnum.empty(Empty())).children.first
// nil

Mirror(reflecting: MyEnum.duplicateEmpty(Empty(), s: Empty())).children.first
// nil

Mirror(reflecting: MyEnum.paddedEmpty(Empty(), NonEmpty())).children.first
// (label: "paddedEmpty", (Empty(), NonEmpty("")))

Mirror(reflecting: MyEnum.noAssociatedValue).children.first
// nil

SR-6921 is partly related, referring to the lack of reflection information for enum cases without associated values.

SR-10272 appears to be an alternative example of an associated value of zero size.

@swift-ci
Copy link
Collaborator Author

Comment by James Froggatt (JIRA)

This issue came up while using EnumKit to access enum associated values via subscripts: gringoireDM/EnumKit#4

@theblixguy
Copy link
Collaborator

Related: SR-12044 which was recently fixed. Can you try with the latest development snapshot?

@swift-ci
Copy link
Collaborator Author

Comment by James Froggatt (JIRA)

The fix works for me as described - label-value pairs are printed for enum cases with zero-size associated values, which fixes SR-10272 and solves my use-case. That's a big win!

As I understand it, enum cases still can't be properly enumerated/compared without making Strings available for both argument labels, and names of cases with no associated values. I imagine a solution would benefit use-cases such as EnumKit and Case Paths. However, preserving argument labels as strings is probably more in the realm of a Swift Evolution proposal. If others agree I'm happy for this ticket to be closed, and SR-6921 can continue to track the no-associated-values behaviour.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants