|
|||||
|
|||||
Typing
MVEL is dynamically typed language, with static typing. Most users of MVEL will rely simply on dynamic typing. It's simple, and easy to work with. a = 10; // declare a variable 'a' b = 15; // declare a variable 'b'; a + b;
Dynamic Typing and CoercionThe most important aspect of a language like MVEL, which interacts directly with native Java objects, which themselves are statically typed, is that of type coercion. Since MVEL cannot truly treat say, a java.lang.String object as a java.lang.Integer object for math operations, it must be able to coerce one type to another. Performance Considerations However, if String variables are being injected into the runtime to be evaluated as Integers, it is simply not possible to prevent the runtime from needing to parse the strings into new Integer objects. This should always be considered. Method Calls Arrays Example: myArray = {1,2,3};
// pass to method that accepts String[]
myObject.someMethod(myArray);
In this particular case, someMethod() accepts a String[] array. This will not fail in MVEL, instead MVEL will convert the array to a String[]. Static TypingStatic typing in MVEL functions just as it does in Java, but by default still works in concert with coercion. int num = 10;
This declares a typed integer variable called num. When you do this, the MVEL runtime will enforce the type. For example, trying to assign an incompatible type after the declaration will result in an exception: num = new HashMap(); // will throw an incompatible typing exception. However, MVEL will perform coercion to a statically typed variable if the value being assigned is coercable. num = "100"; // will work -- parses String to an integer.
Strict TypingStrict typing in MVEL is an optional mode for the compiler, in which all types must be fully qualified, either by declaration or inference. Enabling Strict Mode Satisfying type strictness can be accomplished both by requiring explicit declaration of types inside the expression, or by notifying the parser ahead of time of what the types of certain inputs are. For example: ExpressionCompiler compiler = new ExpressionCompiler(expr); ParserContext context = new ParserContext(); context.setStrictTypeEnforcement(true); context.addInput("message", Message.class); context.addInput("person", Person.class); compiler.compile(context); In this example we inform the compiler that this expression will be accepting two external inputs: message and person and what the types of those inputs are. This allows the compiler to determine if a particular call is safe at compile time, instead of failing at runtime. |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||