Skip to content

Commit dc4faa0

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 2ba0039 commit dc4faa0

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
@@ -413,7 +413,12 @@ private <T> void addItem(final String name, final Class<T> type,
413413
assignAttribute(item, key, value);
414414
}
415415
if (item.isInput()) registerInput(item);
416-
if (item.isOutput()) registerOutput(item);
416+
if (item.isOutput()) {
417+
registerOutput(item);
418+
// NB: Only append the return value as an extra
419+
// output when no explicit outputs are declared.
420+
appendReturnValue = false;
421+
}
417422
}
418423

419424
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)) {
@@ -170,18 +170,14 @@ public void testParameters() {
170170
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, true,
171171
null, null, null, null, null, null, null, null, noChoices, buffer);
172172

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

183179
int outputCount = 0;
184-
final ModuleItem<?>[] outputs = { buffer, result };
180+
final ModuleItem<?>[] outputs = { buffer };
185181
for (final ModuleItem<?> outItem : info.outputs()) {
186182
assertSame(outputs[outputCount++], outItem);
187183
}

0 commit comments

Comments
 (0)