Java Reference
In-Depth Information
jjs > var fut = exc . submit ( function (){ \
java . lang . Thread . sleep ( 10000 ); return 1 ;});
java . lang . RuntimeException : java . lang . NoSuchMethodException : Can ' t
unambiguously select between fixed arity signatures
[( java . lang . Runnable ), ( java . util . concurrent . Callable )] of the method
java . util . concurrent . Executors . FinalizableDelegatedExecutorService
. submit for argument types
[ jdk . nashorn . internal . objects . ScriptFunctionImpl ]
The problem here is that the thread pool has an overloaded submit() method. One
version will accept a Callable and the other will accept a Runnable . Unfortunately,
the JavaScript function is eligible (as a lambda expression) for conversion to both
types. This is where the error message about not being able to “unambiguously
select” comes from. The runtime could choose either, and can't choose between
them.
n
Nashorn's JavaScript Language Extensions
As we've discussed, Nashorn is a completely conformant implementation of ECMA‐
Script 5.1 (as JavaScript is known to the standards body). In addition, however, Nas‐
horn also implements a number of JavaScript language syntax extensions, to make
life easier for the developer. These extensions should be familiar to developers used
to working with JavaScript, and quite a few of them duplicate extensions present in
the Mozilla dialect of JavaScript. Let's take a look at a few of the most common, and
useful, extensions.
Foreach loops
Standard JavaScript does not have an equivalent of Java's foreach loop, but Nashorn
implements the Mozilla syntax for for each in loops, like this:
var jsEngs = [ "Nashorn" , "Rhino" , "V8" , "IonMonkey" , "Nitro" ];
for each ( js in jsEngs ) {
print ( js );
}
Single expression functions
Nashorn also supports another small syntax enhancement, designed to make one-
line functions that comprise a single expression easier to read. If a function (named
or anonymous) comprises just a single expression, then the braces and return state‐
ments can be omitted. In the example that follows, cube() and cube2() are com‐
pletely equivalent functions, but cube() is not normally legal JavaScript syntax:
function cube ( x ) x * x * x ;
function cube2 ( x ) {
return x * x * x ;
}
Search WWH ::




Custom Search