in plain java, can write
class p { static <a> id (a x) { return x; } static int y = p.<integer>id(8); static string bar = p.<string>id("foo"); }
in jshell, can declare , use id
jshell> <a> id (a x) { return x; } | created method id(a) jshell> int x = id(8) x ==> 8 jshell> string y = id("foo") y ==> "foo"
but don't see how make type argument explicit.
jshell> string y = <string>id("foo") | error: | illegal start of expression | string y = <string>id("foo"); | ^
what name of implied context class?
where (part of the) jshell specification allow me answer question? http://openjdk.java.net/jeps/222 mentions "synthetic class" in "the wrapping". not sound named.
indeed link not specify exact nature (like name) of syntetic class gets methods static methods.
i tried class snippet executing in with
jshell> new exception().printstacktrace() java.lang.exception @ repl.$jshell$17.do_it$($jshell$17.java:8) @ jdk.internal.reflect.nativemethodaccessorimpl.invoke0(java.base@9-ea/native method) @ jdk.internal.reflect.nativemethodaccessorimpl.invoke(java.base@9-ea/nativemethodaccessorimpl.java:62) @ jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(java.base@9-ea/delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(java.base@9-ea/method.java:531) @ jdk.internal.jshell.remote.remoteagent.commandloop(jdk.jshell@9-ea/remoteagent.java:124) @ jdk.internal.jshell.remote.remoteagent.main(jdk.jshell@9-ea/remoteagent.java:62) jshell> thread.currentthread().getstacktrace()[1].tostring() $15 ==> "do_it$(java:18)" jshell> thread.currentthread().getstacktrace()[1].getclassname() $16 ==> ""
but can see, information not in stack trace.
the easiest way circumvent it, define method static method in own class:
jshell> class b { static <a> id(a x) {return x;} }
this allows call
jshell> string y = b.<string>id("foo");
and gets desired result.