• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't use +initialize, now what?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't use +initialize, now what?


  • Subject: Re: Can't use +initialize, now what?
  • From: Greg Parker <email@hidden>
  • Date: Wed, 29 Mar 2017 13:41:28 -0700

> On Mar 29, 2017, at 9:02 AM, Charles Srstka <email@hidden> wrote:
>
>> On Mar 29, 2017, at 10:51 AM, Jens Alfke <email@hidden> wrote:
>>
>>> On Mar 29, 2017, at 6:57 AM, Daryle Walker <email@hidden <mailto:email@hidden>> wrote:
>>>
>>> Now the new Xcode release notes say that “• Swift now warns when an NSObject subclass attempts to override the initialize class method, because Swift can't guarantee that the Objective-C method will be called. (28954946)”
>>
>> Huh, I haven’t heard of that. And I’m confused by “the Objective-C method” — what’s that? Not the +initialize method being compiled, because that’s in Swift. The superclass method?
>>
>> Guess I’ll follow that Radar link and read the bug report myself. Oh, wait. :(
>
> You actually can this time, since Swift’s bug reporter is actually open to the public. https://bugs.swift.org/browse/SR-3114 <https://bugs.swift.org/browse/SR-3114>
>
> Basically, with Whole Module Optimization turned on, +initialize wasn’t getting called. Their solution was to just get rid of +initialize.

You don't even need WMO. Many arrangements of Swift code can bypass ObjC +initialize.

Invocation of +initialize is a feature of objc_msgSend(). Swift code does not always use objc_msgSend() when calling methods: some methods are inlined, some are called via virtual table, some are called directly. None of these paths will provoke +initialize.

Changing Swift's generated code to guarantee +initialize would be prohibitively expensive. Imagine an is-initialized check in front of every inlined call. It would be more expensive than +initialize in ObjC because +initialize checking is free once you pay the cost of objc_msgSend(). (The ObjC runtime checks +initialize on uncached dispatch, and never caches anything until +initialize completes. Most dispatches are cached so they pay nothing for +initialize support.)

+initialize in Swift isn't safe and is too expensive to make safe, so we're taking it away instead.


--
Greg Parker     email@hidden <mailto:email@hidden>     Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: Can't use +initialize, now what?
      • From: Charles Srstka <email@hidden>
References: 
 >Can't use +initialize, now what? (From: Daryle Walker <email@hidden>)
 >Re: Can't use +initialize, now what? (From: Jens Alfke <email@hidden>)
 >Re: Can't use +initialize, now what? (From: Charles Srstka <email@hidden>)

  • Prev by Date: Re: Can't use +initialize, now what?
  • Next by Date: Re: Can't use +initialize, now what?
  • Previous by thread: Re: Can't use +initialize, now what?
  • Next by thread: Re: Can't use +initialize, now what?
  • Index(es):
    • Date
    • Thread