Skip to content

Commit 2eb86fb

Browse files
committed
ScriptInfo: append return value if no outputs
We want the return value to be an easy way to return a single, untyped (Object) output. This is quite useful for the "lazy" (I mean "elegant") script writer. But for those consuming scripts, it is annoying to always have to deal with this extra "result" output. As a compromise, let's only append the "result" output if: A) no other outputs were declared; and B) "result" was not declared as an input either.
1 parent 230c267 commit 2eb86fb

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,12 @@ private <T> void addItem(final String name, final Class<T> type,
412412
assignAttribute(item, key, value);
413413
}
414414
if (item.isInput()) registerInput(item);
415-
if (item.isOutput()) registerOutput(item);
415+
if (item.isOutput()) {
416+
registerOutput(item);
417+
// NB: Only append the return value as an extra
418+
// output when no explicit outputs are declared.
419+
appendReturnValue = false;
420+
}
416421
}
417422

418423
private <T> void assignAttribute(final DefaultMutableModuleItem<T> item,

src/test/java/org/scijava/script/ScriptInfoTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testNoisyParameters() throws Exception {
9797
final ScriptModule scriptModule =
9898
scriptService.run("hello.bsizes", script, true).get();
9999

100-
final Object output = scriptModule.getOutput("result");
100+
final Object output = scriptModule.getReturnValue();
101101

102102
if (output == null) fail("null result");
103103
else if (!(output instanceof Integer)) {
@@ -169,18 +169,14 @@ public void testParameters() {
169169
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, true,
170170
null, null, null, null, null, null, null, null, noChoices, buffer);
171171

172-
final ModuleItem<?> result = info.getOutput("result");
173-
assertItem("result", Object.class, null, ItemIO.OUTPUT, true, true, null,
174-
null, null, null, null, null, null, null, noChoices, result);
175-
176172
int inputCount = 0;
177173
final ModuleItem<?>[] inputs = { log, sliderValue, animal, buffer };
178174
for (final ModuleItem<?> inItem : info.inputs()) {
179175
assertSame(inputs[inputCount++], inItem);
180176
}
181177

182178
int outputCount = 0;
183-
final ModuleItem<?>[] outputs = { buffer, result };
179+
final ModuleItem<?>[] outputs = { buffer };
184180
for (final ModuleItem<?> outItem : info.outputs()) {
185181
assertSame(outputs[outputCount++], outItem);
186182
}

0 commit comments

Comments
 (0)