local functions



At last I found some time to read newsgroups... :-)
I have got a nice idea and asking for opinions about it. It looks quite
similar to 'lambda' (as I understand it), but has some additional
features. Well, what do you thinks of it? (hope I have not reinvented
the wheel)

[---------------------- begin ----------------------]
Consider example:

void foo1()
{
.. int i = 0;
.. void comp(int a, int b)
.. { i += a; return a < b; }
..
.. int buf[10];
.. sort(buf, buf + 10, comp);
}

or better:

void foo1()
{
.. int i = 0;
.. template<class T>
.. void comp(T a, T b)
.. { i += a; return a < b; }
..
.. int buf[10];
.. sort(buf, buf + 10, comp);
}

or even more better:

void foo1()
{
.. int i = 0;
.. void comp(int a, int b)
.. {
.. static int j = 0;
.. void foo2(...)
.. { i += b - j; }
.. ... // use foo2
.. j = a;
.. }
..
.. int buf[10];
.. sort(buf, buf + 10, comp);
}

>>From my perspective I do not see any problem with this idea:
- it won't break any existing code
- it is rather useful
- it could be implemented in the same way as member functions
implemented today -- i.e. by passing hidden parameter on the stack
(x86). Overall it is very similar to 'function vs member function'
situation.
- it could be very efficient -- access to one level parent's local
variable equivalent to one level of indirection (which you can not
escape anyway).
- typical visibility rules apply

Possible problems:
- it may not "fit" into existing C++ grammar

Implementation. There are two ways (x86):
1. New type type: 'local function' with sizeof = 16
.. Pro:
.. - you will pay for it only if you use it
.. Con:
.. - every function that receives 'function pointer' will need to have
an additional overload for 'local function' type (or will need typical
clumsy wrapper + mechanism to access and store "current function
context", i.e. function's stack pointer)
2. All function pointers have sizeof = 8 (function pointer + parent's
stack pointer)
.. Pro:
.. - uniformity: function pointer could be passed everywhere
.. Con:
.. - for non-local functions 'context part' will always be unused --
we will pay in runtime for passing/storing data we do not need (this
cost could be too great on a grand scale)

Restrictions -- for the sake of safety these actions should be normally
forbidden/very-hard-to-do:
- assignment of local function pointer/reference to variable with
longer/unpredictable life

[---------------------- end ----------------------]

As I think about this idea, I like it more. It brings a little bit more
of 'uniformity' into C++ (which I miss very much), i.e.:
application scope
{
.. function scope
.. {
.. function scope
.. {
.. ... // and so on
.. }
.. }
}
where every subscope has access to visible part of all parent scopes.
..
..
..
Well, and speaking in terms of improvements anf uniformity, it would be
so good to have:
- 'move' as one of the operations on objects
- uniform variables declarations, i.e. for example:
<modifiers> <type> <var_name> = <value>;
for EVERY type (even for functions)
- making 'function type' equal to other types, 'function' equal to
other variables... e.g.:

{
.. ...
.. <func_type> fn_Foo = { return 15*x; }
.. <func_type>* pfnFoo = new <func_type>({ return 15*x; });
.. assert((*pfn)(10) == 150);
}

In this way it will be easy to create various thunks whenever you need
them (compare how message processing implemented in MFC vs ATL). And if
C++ library will provide conversion functionality:
<func_type> fn_Foo = std::string_to_function("{ return 15*x; }");
then we will have a nice tool to create self-modifying aplication.

Dreams, dreams... Maybe new language? D++? Z@@? ;-)

Bye.
Sincerely yours, Michael.

.