previous | start | next

Restricting access to non-publics to this object

class X
{
   private field y;

   method m(x)
   {
      // any reference to y must be
      // to the private field y
      x.y;
   }
}

Most people find this rule to surprising. It would be too hard to sell to users.

Dave offers an alternative whereby the programmer annotates the reference to make the access explicit:

class Foo
{

   private var f;

   method m (x)
   {
      x.Foo::f   // refers to local f
   }
}

Another possibility is to use a cast, e.g.

((Foo)x).f

Rok proposes we adopt Herman's suggestion that references to other objects from untyped variables is restricted to publics unless there is an annotation such as one of the above two choices, or you are using a statically typed variable.

The consequence of this is that adding the annotation can change whether you get the private field of some object or the public field of its base class.

class Foo extends Bar
{
    public var f;
}

class Bar 
{
    private var f

    method m()
    {
        Foo y = new Foo();

        y.f;         // Foo::f
        y.Bar::f;    // Bar::f
        ((Bar)y).f;  // Bar::f
    }
}



previous | start | next