repl: do not print null is last expression type is unit
This commit is contained in:
@@ -116,8 +116,10 @@ public class ReplFromTerminal {
|
||||
return oneCommand(line.substring(1));
|
||||
}
|
||||
|
||||
Object value = getReplInterpreter().eval(line);
|
||||
System.out.println(value);
|
||||
ReplInterpreter.LineResult lineResult = getReplInterpreter().eval(line);
|
||||
if (!lineResult.isUnit()) {
|
||||
System.out.println(lineResult.getValue());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
|
||||
@@ -118,7 +118,26 @@ public class ReplInterpreter {
|
||||
classLoader = new ReplClassLoader(new URLClassLoader(classpath.toArray(new URL[0])));
|
||||
}
|
||||
|
||||
public Object eval(@NotNull String line) {
|
||||
public static class LineResult {
|
||||
private final Object value;
|
||||
private final boolean unit;
|
||||
|
||||
public LineResult(Object value, boolean unit) {
|
||||
this.value = value;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean isUnit() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public LineResult eval(@NotNull String line) {
|
||||
++lineNumber;
|
||||
|
||||
JvmClassName scriptClassName = JvmClassName.byInternalName("Line" + lineNumber);
|
||||
@@ -173,7 +192,7 @@ public class ReplInterpreter {
|
||||
|
||||
earlierLines.add(new EarlierLine(line, scriptDescriptor, scriptClass, scriptInstance, scriptClassName));
|
||||
|
||||
return rv;
|
||||
return new LineResult(rv, scriptDescriptor.getReturnType().equals(JetStandardClasses.getUnitType()));
|
||||
} catch (Throwable e) {
|
||||
PrintWriter writer = new PrintWriter(System.err);
|
||||
classLoader.dumpClasses(writer);
|
||||
|
||||
@@ -60,7 +60,7 @@ public class ReplInterpreterTest {
|
||||
String code = t.first;
|
||||
String expected = t.second;
|
||||
|
||||
Object actual = repl.eval(code);
|
||||
Object actual = repl.eval(code).getValue();
|
||||
String actualString = actual != null ? actual.toString() : "null";
|
||||
|
||||
Assert.assertEquals("after evaluation of: " + code, actualString, expected);
|
||||
|
||||
Reference in New Issue
Block a user