# Why does Type inference not work in the Processing editor?

**URL:** https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467
**Category:** Processing
**Created:** [April 19, 2019, 7:07pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467 "2019-04-19T19:07:48Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [April 19, 2019, 7:07pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/1 "2019-04-19T19:07:48Z")

</div>

With Type inference I mean

List\<Type\> list = new ArrayList\<\>();

unexpected token: \>  
Why is that?

To provide “further research” (I can feel the toxicity of excessive StackOverflow usage flowing in my veins)

I suspected that maybe Type inference wasn’t supported in all Java versions that have generics.  
So I checked my version with [this post](https://forum.processing.org/two/discussion/9182/which-java-version-does-processing-use-on-my-mac).

Or maybe perhaps this isn’t a feature that the Java compiler provides, but is just the overzealous doing of the Java IDEs I used?

Well, that’s my wit’s end.

---

<div class="post-metadata">

### Author: ![Dennis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/dennis/32/1111_2.png) [@Dennis](https://discourse.processing.org/u/Dennis)
#### Post date: [April 19, 2019, 7:50pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/2 "2019-04-19T19:50:13Z")

</div>

Hi [LoliProcessor](https://discourse.processing.org/u/LoliProcessor),  
I don’t know if the processing implement the inference like go, python or other languages…  
For resolve you question you have to declare `list` inside the symbols

```auto
import java.util.ArrayList;  
import java.util.List;  

List list = new ArrayList<List>();

```

I hope I was helpful…  
Regards.

Processing 3.5.3  
Win10 64bit

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 19, 2019, 9:35pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/3 "2019-04-19T21:35:33Z")

</div>

The reason we can’t use an empty diamond `<>` generic inference is b/c it’s incompatible w/ PDE’s (Processing’s IDE) pre-processor current features! ☹

> <https://github.com/processing/processing/issues/5659>

> <https://github.com/processing/processing/issues/3054>

But I believe we can use the empty diamond in “.java” files, not in “.pde” 1s. ☕

I recall the empty diamond operator is a Java 7 feature AFAIK. 🔶

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 20, 2019, 5:04am UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/4 "2019-04-20T05:04:49Z")

</div>

> [@LoliProcessor](#):
>
> List list = new ArrayList\<\>();

I use

ArrayList list = new ArrayList();

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 20, 2019, 7:07pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/5 "2019-04-20T19:07:18Z")

</div>

Now that you have an answer (it is the partial Java 7/8 preprocessor’s fault)…

Just to confirm – is it that you like the syntactic sugar of not having to repeat the type, or is there something specific you are trying to do that you cannot do with one of the compilable options?

```auto
import java.util.List;
List<PVector> list = new ArrayList<PVector>();

// ...or
List list = new ArrayList<PVector>();
// or
List<PVector> list = new ArrayList();
// or
List list = new ArrayList();

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 20, 2019, 8:27pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/6 "2019-04-20T20:27:21Z")

</div>

> [@jeremydouglass](#):
>
> ```auto
> import java.util.List;
> List<PVector> list = new ArrayList<PVector>();
> 
> // ...or
> List list = new ArrayList<PVector>();
> // or
> List<PVector> list = new ArrayList();
> // or
> List list = new ArrayList();
> 
> ```

2nd & 4th options should be avoided! The others 1st and 3rd are OK. 🤓

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 20, 2019, 8:29pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/7 "2019-04-20T20:29:31Z")

</div>

Well, that’s fair and good to point out – I’m not saying you _should_ do them – I’m just saying they compile.

I think the first is what OP wants – but I can’t be sure not knowing anything about the code context.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 20, 2019, 8:31pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/8 "2019-04-20T20:31:39Z")

</div>

1st 1 is more complete. 3rd 1 just declares generics on the variable only; but it’s enough. 😳

---

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [April 21, 2019, 12:21pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/9 "2019-04-21T12:21:57Z")

</div>

Well, is there a documentation about the preprocessor of Processing (ha!) ?  
Or is the source code available somewhere?

To answer your question, yes, I do like my sugar syntactical.  
I asked the question solely to resolve why Type inference doesn’t seem to work in the Processing editor.

---

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [April 21, 2019, 8:27pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/10 "2019-04-21T20:27:11Z")

</div>

Actually, does the Processing preprocessor also fuck up explicit Type arguments?

```auto
Collections.<Object>emptySet();
Syntax error on token "<", TypeArgumentList1 expected after this token

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 21, 2019, 8:47pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/11 "2019-04-21T20:47:41Z")

</div>

As a workaround, you can type in the PDE “incompatible” syntax inside some “.java” file: ☕

## EmptySet.java

```java
import java.util.Collections;
import java.util.Set;
import processing.core.PVector;

interface EmptySet {
  Set<PVector> emptyVecSet = Collections.<PVector>emptySet();
}

```

Then use it inside your “.pde” files: 😀

## EmptySetSketch.pde:

```java
import java.util.Set;
import java.util.HashSet;

final Set<PVector> empty = EmptySet.emptyVecSet;
final Set<PVector> mySet = new HashSet(empty);

void setup() {
  mySet.add(PVector.random3D(this));
  println(empty, mySet);
  exit();
}

```

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 21, 2019, 8:48pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/12 "2019-04-21T20:48:49Z")

</div>

What the preprocessor reads and writes is (largely) documented in the reference – if not, there is some [documentation of the compiling process](https://processing.org/reference/environment/) and then it is straight to the preprocessor source, which is in [Antlr](https://github.com/processing/processing/tree/349f413a3fb63a75e0b096097a5b0ba7f5565198/java/src/processing/mode/java/preproc). So, if the syntax from Java that you are using appears in the Processing reference or examples, then that is Processing. If it doesn’t, then you can always try and see! Processing is mostly documented declaratively, for learners, like a language – “I can do this” – not differentially, “this is an enumeration of the ways I’m different from Java 7 / 8.” For that, there is mainly experience and forum lore (and the source).

If you don’t WANT to write in Processing – an intentionally simplified syntax that primarily targets beginners and artists – then you can still write Java in .java files that are part of a .pde sketch folder. Or, even better, use an Eclipse project instead of PDE and work with the full Processing API, but not the PDE format or compiler – just write it all in 100% Java, with no pde. It seems like that _might_ be what you want…?

---

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [April 22, 2019, 9:45am UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/13 "2019-04-22T09:45:39Z")

</div>

Then I guess the Folder of your processing sketch (where the data folder is inside as well) acts as the default package of your module.  
But good to know that you can mix .java and .pde files.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 22, 2019, 2:14pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/14 "2019-04-22T14:14:48Z")

</div>

> [@LoliProcessor](#):
>
> acts as the default package of your module.

For more on how that works in PDE (vs eclipse) also read the two “Advanced” note boxes in the documentation I linked above. You can also subclass PApplet within PDE and do all-java there, but your classes are no longer automatically inner classes, so some of the PDE “just works” magic is gone – passing around PApplet is required. At that point it starts making sense to set up the Processing Eclipse Template instead.

[https://processing.org/tutorials/eclipse/](https://processing.org/tutorials/eclipse/)

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 22, 2019, 8:52pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/15 "2019-04-22T20:52:56Z")

</div>

> [@LoliProcessor](#):
>
> Then I guess the folder of your processing sketch… acts as the default `package` of your module.

The PDE concatenates all “.pde” files and transpiles them as 1 valid “.java” file wrapped up as 1 PApplet subclass.

But the PDE doesn’t add the keyword `package` to this unified “.java” file. So it’s kinda “packageless”.

However, we can use the keyword `package` in our own “.java” files, if we really feel like it:

> <https://gist.github.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21>

But even if we do use `package` in our “.java” files, using `import` inside our “.pde” files, in order to access those “.java” sketch files, is still optional.

That is, even though I’ve included `package gotoloop.countdown;` inside “Countdown.java”, that `import gotoloop.countdown.Countdown;` statement inside “CountdownClass.pde” isn’t really needed at all.

---

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [April 23, 2019, 12:35pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/16 "2019-04-23T12:35:27Z")

</div>

Are you sure about your statement about the import statement not being required.  
Using a packaged class without import gives error:  
Cannot find a class or type named “class\_name”

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [April 23, 2019, 12:52pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/17 "2019-04-23T12:52:04Z")

</div>

> [@jeremydouglass](#):
>
> If you don’t WANT to write in Processing – an intentionally simplified syntax that primarily targets beginners and artists

The decision to change language syntax, as opposed to just structural syntax, is probably the worst decision Processing made. It’s what makes adopting Java 7/8 features like this more difficult, and ends up with Java syntax actually being the simpler one. Maybe with Java 11 (and additional type inference like `var`) it would be good to either remove the bulk of the preprocessor, or provide an alternative mode?

For comparison, PraxisLIVE uses Java basic syntax, but the same basic structural wrapping (import, class body, etc.) as Processing using preprocessing code from Janino that has most of the same benefits with less code, and handles Java language updates without problem.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [April 23, 2019, 2:05pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/18 "2019-04-23T14:05:21Z")

</div>

> [@LoliProcessor](#):
>
> Using a packaged class without import gives error:

Oh, you’re right! Perhaps that was true in some very old PDE version. 🥴

Indeed, if we use `package` in a “.java” file, we’ve gotta use `import` for it. 🙄

Otherwise, if they don’t have `package`, we can straight use them! 🤩

---

<div class="post-metadata">

### Author: ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)
#### Post date: [May 1, 2019, 2:49pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/19 "2019-05-01T14:49:01Z")

</div>

I am really not trying to pick on Processing here. In fact, I think Processing is pretty cool.

But is it possible that wildcard extensions don’t work in the Processing editor either?

Wildcard Extension:

ParameterizableClass\<? extends TYPE\> a;

TYPE objectOfTYPE = new TYPE();

a.parameterizedMethod(objectOfTYPE); // prePROCESSES to an error that the capture of a type doesn’t equal the type itself

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [May 1, 2019, 4:24pm UTC](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467/20 "2019-05-01T16:24:42Z")

</div>

Ru sure that’s valid Java syntax? I’ve made the attempt code below in order to check that out: ✍

```auto
import java.util.List;
import java.util.ArrayList;

public class Test {
  public final List<? extends Double> list = new ArrayList<>();
  public final Double d = Math.random();

  public Test() {
    //list.add(d); // uncomment this for "capture" bug!
  }

  @Override public String toString() {
    return "" + d;
  }

  @SafeVarargs public static final void main(final String... args) {
    System.out.println(new Test());
  }
}

```

Pasted that code at this Java online IDE site (javac 1.8.0\_201): 🗒  
[https://www.CompileJava.net](https://www.CompileJava.net)

And here’s the error log if we uncomment `list.add(d);` inside Test’s constructor: ☠

```auto
/tmp/java_3Bq5e5/Test.java:9: error: no suitable method found for add(Double)
    list.add(d); // uncomment this for bug!
        ^
    method Collection.add(CAP#1) is not applicable
      (argument mismatch; Double cannot be converted to CAP#1)
    method List.add(CAP#1) is not applicable
      (argument mismatch; Double cannot be converted to CAP#1)
  where CAP#1 is a fresh type-variable:

```

[Next page](https://discourse.processing.org/t/why-does-type-inference-not-work-in-the-processing-editor/10467.md?page=2)
