Issue
How to write a Java function which simply takes an argument of any type, prints it and returns it?
In Scheme I am used to write the following macro:
(define-syntax dump
(syntax-rules ()
((_ arg) (let ((value arg))
(display 'arg)
(display " -> ")
(display value)
(newline)
arg))))
Calling it (+ 1 (dump (* 2 3))) would return 7 and print (* 2 3) -> 6.
I tried something similar in Java:
public class Debug<T>
{
public static T dump (T arg)
{
System.err.println (arg);
return arg;
}
}
But I get the error:
non-static class T cannot be referenced from a static context
How to solve this?
Solution
You implemented your class as an inner class inside some other. In that case you must mark the class as static.
But, the real answer is: you don't need the class at all, you just need the method:
public static <T> T dump(T arg) { ... }
As a side note, I use the same trick in my code, but I always include a msg argument for easier dump reading/grepping:
public static <T> T dump(String msg, T arg) { ... }
Answered By - Marko Topolnik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.