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-3006] CharacterSet should have a way of ensuring the wrapped NSCharacterSet is immutable #3919

Open
lilyball mannequin opened this issue Oct 20, 2016 · 6 comments

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Oct 20, 2016

Previous ID SR-3006
Radar None
Original Reporter @lilyball
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Improvement
Assignee None
Priority Medium

md5: 0db672c16638232d28397c95f5e0c65c

relates to:

  • SR-9044 Mutated CharacterSet bridges to Obj-C as NSMutableCharacterSet

Issue Description:

According to the documentation on NSMutableCharacterSet, mutable character sets are less efficient to work with than immutable character sets (assuming no more mutations need to happen), and therefore it recommends calling copy() to produce an immutable character set once you've finished mutating it.

However, there appears to be no way to get this behavior with CharacterSet, short of bridging to Obj-C, copying, and bridging back.

To that end, it would seem to be useful for CharacterSet to have a method that ensures the wrapped character set is immutable.

@belkadan
Copy link

I would hope it's already doing that at bridge-time; otherwise it's not a proper value type.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Oct 21, 2016

Even if just bridging to Obj-C and back (without calling copy) is sufficient (and you're right, it probably should be doing that already), that's still something that shouldn't be required in order to get efficient character sets.

@belkadan
Copy link

Ah, right, I forgot about mutating operations.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Oct 19, 2018

I just checked, bridging to Obj-C actually produces an NSMutableCharacterSet. This seems like a problem. And I confirmed, it's definitely not doing a mutable copy under the hood, as mutating this NSMutableCharacterSet modifies the original CharacterSet.

I'll file a separate ticket for this issue, but it does confirm that bridging to Obj-C and back won't "freeze" a CharacterSet, so I still need the very awkward unsafeDowncast((cs as NSCharacterSet).copy() as AnyObject, to: NSCharacterSet.self) as CharacterSet.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Oct 19, 2018

I filed the followup ticket as SR-9044. I also put a long comment on that ticket that describes a new approach to constructing {{CharacterSet}} that avoids this issue.

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Oct 19, 2018

Update: Bridging to Obj-C and back actually should "freeze" it because the act of bridging it back will copy it. Assuming of course that Swift doesn't see cs as NSCharacterSet as CharacterSet and just optimize away the bridging entirely.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant