www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Suggestion: adding stubs to function definition

I am not an expert in languages ( I don't know neither english, it is no=
t  =

my language :-) ),
but I think that adding a "stub" section in function definition can make=
 D  =


Consider this example:

///this function returns the integer part of the square root of its inpu=
int isqrt(int x)
out(y) {
	assert( y*y <=3D x );
	assert( (y+1)*(y+1) >=3D x);
	if(x<4) return 1;
	if(x<9) return 2;
       if(x<16) return 3;
body {

the stub section is optional, and if there is a stub section, the body  =

section is optional.
Probably there is the need for a fixed structure in a stub section, mayb=
e  =

allowing only
if-statements, return and accessing simple variables (calling a function=
,  =

or using loops must be disallowed ).
The stub section can be used for:

1) prototyping: if writing the function body is complex,it is possible  =

initially to  write a simple stub

2) debugging: when there are both a stub section and a body section, it =
is  =

possible the compare the results returned by both at compile time (in  =

simple cases).

3) optimization: the information in the stub section can be used for  =

optimize the code generated.
For example if somewhere there is the code


the compiler can convert this code to z=3D30/2; and then to z=3D15;

I am aware that stubs do not add new functions to D, and also that they =

can add complexity to the compiler, because I think that it is not easy =
to  =

extract the information for 2) and 3) in the general case (for example i=
f  =

the input parameters of the function are objects or complex types), but =

there are cases in which adding the functionality 2 and 3 is trivial.

-- =

Creato con il rivoluzionario client e-mail di Opera:  =

Aug 27 2006