Difference between revisions of "DIP55"

From D Wiki
Jump to: navigation, search
(Abstract)
(Abstract)
Line 244: Line 244:
  
 
E.g. for traversing trees the stack can be better incorporated into the algorithm implementation. A parent/child relationship is supported by a caller/called model.
 
E.g. for traversing trees the stack can be better incorporated into the algorithm implementation. A parent/child relationship is supported by a caller/called model.
 +
 +
(see e.g. [https://github.com/FBergemann/ASF-RBT/tree/for_delete ASF RBT] as a trial to make use of this programming model for a Red/Black-Tree algorithm - w/o need for a parent ptr in the node structure (however it is incomplete - the delete operation is still missing))
  
 
== Rationale ==
 
== Rationale ==

Revision as of 14:35, 1 January 2014

Title: Access Data Items In Ancestor Stack Frames
DIP: 55
Version: 1
Status: Draft (under construction)
Created: 2014-01-01
Last Modified: 2014-01-01
Author: Frank Bergemann
Links: Dr.Dobb's Article Access Data Items In Ancestor Stack Frames Safely

NG discussion for C++

Abstract

The language should support an implicit *caller pointer for functions (callee) for access to the calling function (caller).

As if functions would internally be modelled as class wrappers (functors) passing their *this ptr implicitly to a called function.

(Likely this behavior is switched on/off, auto-detected by making use of the *caller ptr in a called function yes/no.)

Rationale

1. callee pulls data specificially when/if required instead of caller pushs all always

This might be a performance improvement.

f1(void)
{
   SomeType x;
   SomeType y;
   SomeType z;
   [...]
   f2();
   [...]
}

f2(void)
{
   if (caller->x.IsValid())
   {
      use(caller->y)
   }
   else 
   {
      use(caller->z);
   }
}

2. public, private (protected?) caller data

Like for data members for classes, local variables of functions might be declared private, public (protected?) for access limitations

f1(void)
{
private:
   int _x;
   int _y;
public:
   int z;
   [...]
   f2();
   [...]
}

f2(void)
{
private: 
   int _a1 = caller->_x * 2.0; // ERROR!
   int _a2 = caller->z * 2.0;  // OK
   [...]
}

3. non-const for the caller, const for the callee

f1(void)
{
private:
   int _x;
   int _y;
public:
   const int & x = _x;
   const int & y = _y;
   [...]
   f2();
   [...]
}

f2(void)
{
private: 
   caller->x *=  2.0;        // ERROR!
   int x2 = caller->x * 2.0; // OK
   [...]
}

4. public, private (protected?) local functions

Such might serve as getter or setters with similar benefits we have for accessing class data.

f1(void)
{
private:
   SomeProtoType _x;
public:
   SomeType GetX()(void)
   {
      return SomeType(_x);
   }

   [...]
   f2();
   [...]
}

f2(void)
{
   if (whatsoever())
   {
      DoIt(caller->GetX());
   }
}

E.g....

5. Lazy evaluation

Data could be prepared only by caller for callee. But it could be up to the callee to decide if it actually wants that data and for this delay its calculation.

f1(void)
{
   SomeType x;
   SomeType y;
   SomeType z;
   [...]
   f2();
   [...]
}

f2(void)
{
   if (caller->x.IsValid())
   {
      caller->y.ExpensivePreProcess();
      use(caller->y);
   }
   else 
   {
      caller->z.ExpensivePreProcess();
      use(caller->z);
   }
}

6. Multi-level

f1(void)
{
public:
   RootData rootData;
private:
   [...]
   f2();
   [...]
}

f2(void)
{
public:
   SomeData x;
   SomeData y;
   SomeData z;

   f3();
}

f3(void)
{
   if (caller->x.IsValid())
   {
      use(caller->caller->rootData, caller->y)
   }
   else 
   {
      use(caller->caller->rootData, caller->z)
   }
}

Description

STILL TEMPLATE CONTENT ONLY HERE

This section has been adapted for MediaWiki.

A DIP is a D Improvement Proposal, a way propose changes to the language in a sightly more formal way than just throwing out the idea in the news group. A DIP should have an structure similar to this one, but nothing is set in stone, you can add or remove sections and/or metadata as long as the DIP is both clean and complete, but try to stick to this template as a bare minimum unless is really necessary to remove something.

A DIP should represent a problem the community wants to resolve and not just a specific resolution to a problem. This allows the DIP to be a central hub for any given problem. If a resolution is radically different from the current state of the DIP, an alternative DIP could be created as a sub page, e.g. under /DIP1/Alternatives/Alt1. The DIP should be created in its entirety such that it could replace the current DIP through simple copy and past.

Usage

STILL TEMPLATE CONTENT ONLY HERE

This section has been adapted for MediaWiki.

To start a new DIP you can go to Edit link and copy the source of this DIP, then go to DIP index and add a new item in the list. The DIP number should be one more than the last DIP in the index (for example, if the DIP1 is the last DIP, your DIP should be DIP2). The link in the index should have the form: [[DIPx]], Title, Status, resume. Where x is the DIP number, title is the DIP title and resume is a short description about the DIP.

Save the DIP index page and click on the new red link. Now you are editing the new DIP you just created, now paste the copied source text from this template and replace all the you need.

Remember to update the metadata at the start of the DIP, and keep it as a Draft at the beginning. When your DIP is done, you should announce it in the News Group for discussion, with a subject like this: new DIPx: title (where one more time x is the DIP number and title is the DIP title).

You should always put you DIPs in the Public Domain (or a similarly permissive license but use Public Domain unless you're very sure of what you're doing).

Recommendations

STILL TEMPLATE CONTENT ONLY HERE

When writing a DIP, try not to express your opinion. DIPs should provide facts and be as objective as possible. Even when that's pretty hard, you can make the DIP look more objective by not using, for example, "I prefer XXX because YYY". If YYY is an objective advantage, write that in a more objective way, like "XXX can be a better option because YYY". Try to leave non-technical personal preferences aside; "XXX can be a better option because the syntax is nicer" is not good enough even when you don't say "I prefer".

Try not to include half-baked ideas. If you are not sure about something, leave it outside the DIP and write it on the NG announcement instead for further discussion. The idea can be added to the DIP in the future when it is in a better shape.

Abstract

This CR enables features used for classes (structural) to functions/procedures (procedural).

As it shares data along a call hierarchy it can be considered a "better version of global variables" - because it is limited to the execution scope (call hierarchy).

It offers a toolbox to make better use of a stack. It adds a stack-machine user interface for the programmer (which might be usable for compilers?).

E.g. for traversing trees the stack can be better incorporated into the algorithm implementation. A parent/child relationship is supported by a caller/called model.

(see e.g. ASF RBT as a trial to make use of this programming model for a Red/Black-Tree algorithm - w/o need for a parent ptr in the node structure (however it is incomplete - the delete operation is still missing))

Rationale

STILL TEMPLATE CONTENT ONLY HERE

Rationale should be complete. When the DIP tries to solve a problem, try to describe that problem as detailed as possible. If you have links to the NG describing the problem more deeply, used them. All the background information is welcome.

NG Announcement

STILL TEMPLATE CONTENT ONLY HERE

When posting the DIP announcement to the NG, please copy the abstract, so people can easily know what is it about and follow the link if they are interested.

Copyright

This document has been placed in the Public Domain.