myWishList

Tom Kaitchuck <[email protected]> Fri, 17 Jun 2005 11:10:17 -0700
Newsgroups gmane.comp.lang.nice.general
Message-ID <[email protected]>
I also have a few wish list items for nice.
First I think it would be better if multimethods had a more normal 
syntax. IE: Outside of a class I could declare:

int Object.toInt() throws NumberFormatException
{
    Integer i = Integer.decode(this.toSting());
    return i.intValue;
}

Then this would add a function to the object class. It would be also 
good if this function would be included in JavaDocs/NiceDocs. The 
hardest part would be how to handle includes... I guess it would get 
imported if you import the class of the name of the file it is attached 
to...

Another thing I would find really useful is to somehow get the benefits 
of macro preprocessing. Now, I understand why Java did not include a 
preprocessor, and what a pain it can make things. However I often find 
myself writing the same patterns over and over, for example:
if(logger.shouldlog(Levels.DEBUG)
{
    logger.debug(expenciveFunctionCall());
}

or this:

while (notReady())
{
    try {
    someObject.wait();
    } catch (InteruptedException e) {}
}

These situations could be avoided if we could create "Inlineable 
functions". These would be functions you could declare a function in a 
class, just like any other function, but that could take an expression 
as an argument. The expression has a variable associated with it. So 
when the function executes, any instances of the variable are replaced 
with the function that gets passed (Of course it would have to check the 
returned type). What happens internally is that this function simply 
gets inclined into all the places that call it.

This would not have all the headaches of a full macro preprocessor, 
because it could not create anything invalid without it being detected 
(and easily fixed) at compile time. Privileges would not be an issue, 
just treat it like any other function, the expression can only access 
things the caller can access and the rest of the function can only 
access things it's class can access.
Here is an example of what this might look like:
In the logger class, I change the debug function to read:

//Keyword inlineable means this function will get inlined into it's caller.
inlineable void debug (expression<String> expr) throws Exception
{ //expr is a valid java expression that will evaluate to a String.
    if(shouldlog(Levels.DEBUG)
    {
        logThis(expr); //Only now is expr evaluated. Because we know it 
returns a string, it can't cause problems.
        //We have to throw or handle an exception because expr could 
throw one.
    }
}

Then the caller would just do:
logger.debug(expenciveFunctionCall());
And they don't have to know how it works.

There are of course other ways the syntax could work, but I think that 
this could provide most of the benefits of having a macro processor like 
in c or c++ but without allowing it to introduce errors into the code.

I don't have a lot of time available, but I do know Java well, and would 
be willing to help implement this.


-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click