Skip to content

Commit f115b32

Browse files
committed
CallingOps: improve wildcards section
1 parent d71627f commit f115b32

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

docs/ops/doc/CallingOps.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,31 @@ direct execution, since the parameters have not been concretely specified yet.
7272
Using [wildcards](https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html), such as `Img<?> inImage`, can make Op reuse difficult. For example, the following code segment will not compile in a Java runtime:
7373

7474
```java
75-
Img<?> inImage = ...;
75+
Img<?> inImage = ArrayImgs.unsignedBytes(128, 128);
7676
var gaussOp = ops.op("filter.gauss").input(inImage, 2.0).output(outImage).computer();
7777
gaussOp.compute(inImage, 2.0, outImage);
7878
```
7979

8080
### Solution 1: Use `compute` instead of `computer`
8181

82-
If you don't need to save the Op to a variable, *just call it directly* as shown [here](#computing-with-compute). Generally speaking, op requests are **cached**, meaning repeated OpBuilder calls that directly execute Ops will **not** significantly increase performance.
82+
If you don't need to save the Op to a variable, *just [call it directly](#computing-with-compute)*:
8383

84-
### Solution 2: Use Type Parameters on your functions
84+
```java
85+
ops.op("filter.gauss").input(inImage, 2.0).output(outImage).compute();
86+
```
87+
88+
Generally speaking, op requests are **cached**, meaning repeated OpBuilder calls that directly execute Ops will **not** significantly decrease performance.
89+
90+
### Solution 2: Avoid using wildcards
91+
92+
If you *know* that your `Img` will always contain unsigned byte values, for example, define your variable as an `Img<UnsignedByteType>` rather than using `Img<?>`.
93+
94+
### Solution 3: Use raw casts (not type-safe!)
95+
96+
```java
97+
Img<?> inImage = ArrayImgs.unsignedBytes(128, 128);
98+
var gaussOp = ops.op("filter.gauss").input(inImage, 2.0).output(outImage).computer();
99+
gaussOp.compute((Img) inImage, 2.0, outImage);
100+
```
85101

86-
If you *know* that your `Img` will always contain bytes, define your variable as an `Img<ByteType>`
102+
This method should only be used as a last resort.

0 commit comments

Comments
 (0)