Feature Request: Expose the context pointer of nested classes, please

icee iceelyne at gmail.com
Fri Sep 29 07:05:53 PDT 2006


Keyword for direct access of the outer from the inner of nested classes, the 
"context pointer".

"
Non-static nested classes work by containing an extra hidden member (called 
the context pointer) that

is the frame pointer of the enclosing function if it is nested inside a 
function, or the this of the enclosing

class's instance if it is nested inside a class.
When a non-static nested class is instantiated, the context pointer is 
assigned before the class's

constructor is called, therefore the constructor has full access to the 
enclosing variables.
"
these words are from the document of D Programming Language.

I know this topic has been discussed some times, but i strongly like feature 
to be added in the future.
I thought it is not reasonable if you can access all members of the outer 
class but not itself from within

the inner class.
Some one suggest the solution of creating a field and assigning this to it 
in the outer ctor:
class Outer {
 Outer ctx;
 class Inner {
  void foo() {
   bar(ctx);
  }
 }
 this() {
  ctx=this;
 }
}
but it's not much convenient.

Since the "non-static nested classes work by containing an extra hidden 
member (called the context

pointer)", why not just expose it to us?

""" if we have such a keyword: context / ctx / enclosure / outer or 
somewhat?

here a real example, from my DWT app codes, (that's why i want this?)

class QKDataTab : CTabFolder {

 class ScriptTabItem : CTabItem {
  Text script;
  this(QKDataTab parent) {
   super(parent, DWT.CLOSE);
   setImage(_app.getIcon("demo"));
   setControl(script=new Text(parent,

DWT.MULTI|DWT.V_SCROLL|DWT.BORDER));
  }
  ...
 }
 /// if we have "enclosure" here, we can rewrite the above as:
 ///class ScriptTabItem : CTabItem {
 /// Text script;
 /// this() {
 ///  super(enclosure, DWT.CLOSE);
 ///  setImage(_app.getIcon("demo"));
 ///  setControl(script=new Text(enclosure,

DWT.MULTI|DWT.V_SCROLL|DWT.BORDER));
 /// }
  ...
 ///}
 ///that's much reasonable.

 QKApp _app;

 this(Composite parent, QKApp app) {
  _app = app;
  super(parent, DWT.TOP|DWT.BORDER);
  setSimple(true);
  initComponents();
 }
 void initComponents() {
  ...
 }

 ScriptTabItem addScriptTabItem(char[] title) {
  ScriptTabItem sti = new ScriptTabItem(this);
  sti.setText(title);
  setSelection(sti);
  _app.mainToolBar.enableItemExecute();
  return sti;
 }
 ScriptTabItem getSelectionScript() {
  return cast(ScriptTabItem)getSelection();
 }
 ...

}

some thought?





More information about the Digitalmars-d mailing list