Suggestion: adding stubs to function definition
claudio fanelli
digitalmars1.20.cfan_84084b at spamgourmet.com
Sun Aug 27 07:57:56 PDT 2006
I am not an expert in languages ( I don't know neither english, it is not
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 input
int isqrt(int x)
in{
assert(x0=>0);
}
out(y) {
assert( y*y <= x );
assert( (y+1)*(y+1) >= 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, maybe
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=30/isqrt(7);
the compiler can convert this code to z=30/2; and then to z=15;
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 if
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/
More information about the Digitalmars-d
mailing list