Re: static acting like __block in GCD singleton pattern
Re: static acting like __block in GCD singleton pattern
- Subject: Re: static acting like __block in GCD singleton pattern
- From: Matt Neuburg <email@hidden>
- Date: Sat, 29 Jun 2013 09:20:22 -0700
On Jun 28, 2013, at 5:26 PM, Kyle Sluder <email@hidden> wrote:
> On Fri, Jun 28, 2013, at 05:17 PM, Matt Neuburg wrote:
>> Why is the block permitted to assign to the variable sharedInstance
>> outside the block? Evidently it is because "static" has an effect like
>> "__block", in that it makes the variable outside the block assignable.
>> But how, exactly? Is it some kind of side effect of being a local static?
>
> Yeah, static storage doesn't need to be captured.
>
> There's no difference between a local or global variable with static
> storage other than the scopes in which the variable can be referred to.
>
> Consult the blocks spec for more:
> http://clang.llvm.org/docs/BlockLanguageSpec.html
>
Yes, I looked at the spec and searched on the word "static" but I can't really understand what the spec is telling me. Am I the only one who thinks that this document has gotten more and more obscure and abstruse over time? The chief effect of the section on the __block qualifier was to make my eyes cross. It never got as far as my brain.
I take it that the distinction we're drawing here is between static storage (which happens at file level and is therefore permanent) and automatic storage (which is limited to a scope, and dies when the scope dies). But I'm not clear on *why* that makes the difference. I guess it's because an automatic variable might die before the block can ever execute, but a static variable won't. But then I'm still a little confused about which value of a static the block will use, the value at the time of definition or the value at the time of execution.
I'm trying to come up with a pithy explanation, suitable for beginners, of why a "static" variable doesn't need a "__block" specifier in order for a block to assign to it. The word "captured", as you suggest, may be useful here, since this embodies the notion that an automatic variable is copied as const (and so can't be assigned to) in the context of the block. Thx - m.
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
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