On May 30, 2009, at 10:05 PM, Joe Strout wrote:
William Squires wrote:
A protected property can be accessed via code in IT'S instance,
or in code in a subclass method (which would refer to its
inherited property), but not in other code.
That appears to be a fair description of how RB's compiler
currently works, but there is no reason that it SHOULD work that
way.
That is, in the code in Class2, you can refer to Property1
without an accessor, but you'll be referring to the value of
Property1 associated with the instance of Class2 that you
created with New. A different instance of Class2 cannot access
the Property1 in the first instance without an accessor!!
And this isn't even true, since you can trivially access the
other object's Property1 simply by typecasting it to a Class1.
This should be a bug! :)
I don't agree. Protected and private are not about restricting
access to some properties or methods to the particular objects that
own them. Rather, they are about restricting access to the CLASS
that owns them. That's why, for example, you can have a shared
class method (which is not associated with any instance at all!)
that accesses protected private members of objects of its class.
In the same way, one object can always access the privates of
another member of the same class. Both of these behaviors are quite
standard and very useful (in factory functions or for making
comparison operators, for example).
No, a shared class method should only be able to access a shared
class property, not an instance variable of a specific instance of
that class. After all, it has no knowledge of a specific instance
(that's the whole point), so how could it access an instance variable?
If you want a method of some class to be able to access a
protected or private instance variable of an instance of some other
class, then that class needs to expose it through an accessor method,
or a non-default constructor.
The quirk that Karen found is that, if the static type of the
reference is a subclass rather than the base class, code in the
base class is not being allowed access to the base class privates.
And THAT is the bug (or at best, design flaw).
Best,
- Joe
I assume the code in question isn't in an overridden subclass method?
If so, then I would say that's a bug.
--
Joe Strout
Inspiring Applications, Inc.
http://www.InspiringApps.com
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>
|