|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
digitalmars.D - Feature Request: Expose the context pointer of nested classes, please
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?
Sep 29 2006
icee wrote:Keyword for direct access of the outer from the inner of nested classes, the "context pointer". Sep 29 2006
Ivan Senji wrote:icee wrote:Keyword for direct access of the outer from the inner of nested classes, the "context pointer". Sep 29 2006
Tom S wrote:Ivan Senji wrote:icee wrote:Keyword for direct access of the outer from the inner of nested classes, the "context pointer". Sep 29 2006
"Pragma" <ericanderton yahoo.removeme.com> wrote in message news:efjrcf$ihv$1 digitaldaemon.com...Tom S wrote:Ivan Senji wrote:icee wrote:Keyword for direct access of the outer from the inner of nested classes, the "context pointer". Sep 29 2006
Java use explicit class name to access outer this, like "Outer.this", but I think D can go beyond this by using a keyword. the Only problem may be to find a keyword which we both agree on and is not likely to mess the existing codes. I prefer "ctx_this" which is rarely used, i think. thix?? Sep 30 2006
icee wrote:Java use explicit class name to access outer this, like "Outer.this", but I think D can go beyond this by using a keyword. the Only problem may be to find a keyword which we both agree on and is not likely to mess the existing codes. I prefer "ctx_this" which is rarely used, i think. Sep 30 2006
Uh, "ctx_this" is ugly, what is wrong with just "outer"? Oct 01 2006
DMD .168 adds the .outer property, so it's resolved, thanks. Oct 04 2006
|