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  =

better.

Consider this example:

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

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

    z=3D30/isqrt(7);

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.
Greetings.

-- =

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

http://www.opera.com/mail/
Aug 27 2006