At 9:20 AM -0700 6/29/13, Matt Neuburg wrote:
On Jun 28, 2013, at 5:26 PM, Kyle Sluder <k...@ksluder.com> 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?
 >
 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.

Hi Matt,

Thanks for asking the question and getting me to read the spec -- I've been trying to get this straight in my own head as well.

I found this part of the spec pretty readable (it's the minutia about declarations that makes my eyes glaze over).

What I think you wanted to be searching for is the opposite of static: "automatic".

The magic 3 paragraphs are:

The compound statement body establishes a new lexical scope within that of its parent. Variables used within the scope of the compound statement are bound to the Block in the normal manner with the exception of those in automatic (stack) storage. Thus one may access functions and global variables as one would expect, as well as static local variables.

Local automatic (stack) variables referenced within the compound statement of a Block are imported and captured by the Block as const copies. The capture (binding) is performed at the time of the Block literal expression evaluation.

and

In addition to the new Block type we also introduce a new storage qualifier, __block, for local variables. The __block storage qualifier is mutually exclusive to the existing local storage qualifiers auto, register, and static.

Variables qualified by __block act as if they were in allocated storage and this storage is automatically recovered after last use of said variable.

----------------------------

Here's my shot at simple rules:

1) With the exception of automatic (stack-based) variables declared outside of a block and used within it, everything works the same as it always has.

2) __block variables act as if all copies of the block (and the original scope) share a reference to the variable which lives as long as the last copy.

3) The block's view of all non-__block automatic variables is read-only and frozen at the time that the block is declared.

----------------------------

I think that's sufficient.

Everything else is either an implementation detail or an optimization

So, to answer your original question: static falls within 1) and works the way it always has because it's not an automatic variable.

Rather than make this any longer, feel free to poke holes in the description -- I want to make sure I'm not missing anything.

HTH,

-Steve




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to