Reputation: 8502
It it possible to create a locally scoped variable in a method for the lifetime of it's class instance?
I can't use static
because it would be shared across all class instances, which is no good. In essence, I want to protect an instance variable by making it so that can only be accessed by one particular method in the class.
I'm thinking this can't really be done, but I thought I'd ask cause it would help a lot.
UPDATE
So, I'm marking @dasblinkenlight's answer as correct (and it is). Although @joshcaswell provided a reference to a better answer in the comments, which is an actual implementation of associative references by Richard J. Ross III. This provides a much easier way of associating a reference without having to dive into the runtime (because he does it for you). If I had found this answer during my original search I wouldn't have asked this question in the first place. But I didn't, so I did.
Why Do this?
I came across an article by Brent Simmons today called Why is Using Associated Objects a Hack?, which directly relates to the question of why I would need to do this in the first place (@bbum & @GabrielePetronella asks this). In my commented discussion with @dasblinkenlight, I explain I want to protect a lazily instantiated iVar from being accessed directly except by it's constructor method. The best way to do this is to hide the iVar from all other class methods so it can only be accessed through it's constructor method. You can, of course, rely on naming conventions (i.e.: _lazyiVar) to remind you not to access it directly, but this is (IMO) a rather fragile form of protection (and I have done this exact thing in the past). Using associated references is a hack and you should question techniques that access the runtime directly, but in this case I prefer the hack as it makes my code that much less likely to crash by protecting my lazily instantiated variables.
Upvotes: 3
Views: 157
Reputation: 726579
You are correct, this is not possible: locals cannot outlast the scope in which they are defined. You can add "private"-ish instance variables through class extensions, but these would be accessible to implementations of all methods, not just a single one.
You can also fake addition of instance variables with associative references, but that would require direct interaction with runtime. These references would be accessible to all methods as well, but you can "hide" them by making their static ObjectTagKey
local to your method.
Upvotes: 4