help-jel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-jel] Stateless method evaluation


From: Mark Taylor
Subject: [Help-jel] Stateless method evaluation
Date: Mon, 11 Feb 2019 11:08:29 +0000

Dear Konstantin,

I have a question about evaluation of stateless methods in JEL.
The documentation says this:

   "Methods and variables of the first class are assumed (by default) to
    be dependent only on their arguments i.e. not to save any information
    from call to call (they are "stateless")... Examples are mathematical
    functions like sin, cos, log, constants E, PI in java.lang.Math. For
    such methods (fields) it does not matter how many times (when)
    they will be called (their value will be taken) the result will
    always be the same provided arguments (if they are present) are the
    same. Stateless methods will be evaluated by JEL at compile time if
    their arguments are constants (known at compile time)."

If I run the following code:

    import gnu.jel.CompiledExpression;
    import gnu.jel.Evaluator;
    import gnu.jel.Library;

    public class JelEval {
        public static void main( String[] args ) throws Throwable {
            String sexpr = "identity(intObj(3))";
            Library lib = new Library( new Class[] { JelEval.ALib.class },
                                       null, null, null, null );
            CompiledExpression compex = Evaluator.compile( sexpr, lib );
            System.out.println( sexpr );
            for ( int i = 0; i < 4; i++ ) {
                System.out.println( "\t" + compex.evaluate( null ) );
            }
        }

        public static class ALib {
            public static String identity( Object o ) {
                return Integer.toHexString( System.identityHashCode( o ) );
            }
            public static Integer intObj( int i ) {
                return new Integer( i );
            }
            public static double[] array3( double x1, double x2, double x3 ) {
                return new double[] { x1, x2, x3 };
            }
            public static double[] array( double... values ) {
                return values;
            }
        }
    }

I get this output:

    identity(intObj(3))
            67386000
            2626d4f1
            3f662dc8
            4d47c5fc

Given the last sentence of the manual excerpt I quote above, I would
expect the program to print the same identityHashCode four times,
rather than four different ones.  Does the evaluation at compile time
only apply to primitive return values, or is there something else I
don't understand here?  If I haven't stated the problem clearly enough,
please let me know and I'll try to be more explicit.

Many thanks in advance,

Mark

--
Mark Taylor   Astronomical Programmer   Physics, Bristol University, UK
address@hidden +44-117-9288776  http://www.star.bris.ac.uk/~mbt/



reply via email to

[Prev in Thread] Current Thread [Next in Thread]