question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

use PECS principle in TreeTraverser

See original GitHub issue

Currently, TreeTraverser<T> has a method Iterable<T> children(T root). Since for traversal, the returned Iterable is only used as a producer, the return type can be Iterable<? extends T> (and the using() static function can take a Function<T, ? extends Iterable<? extends T>>).

The use case is a tree model where at some level only leaf nodes exist, so there is for example a List<LeafNode> (where LeafNode extends T). With the current implementation, this has to be copied into a new list in order to compile (or using some ugly unsafe double-casting).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
HermanBovenscommented, Apr 12, 2017

@kevinb9n, Personally I feel that users who test with such fine granularity have to accept the extra brittleness, certainly for an API that is in beta. After all the fix is trivial (just change the variable type). Of course, they could also just pass a Java8 method reference to TreeTraverser.using(), and then test the method directly.

1reaction
HermanBovenscommented, Apr 12, 2017

Indeed, my uses cases were along the lines of the example that @perceptron8 has shown. Another possibility (besides having the wildcard in the superclass’s return type for getChildren()) is that the function to map parent to children uses instanceof and a (clean, safe) cast to distinguish between different types, some of which may return a specific type of children.

Regarding the use of Set vs Iterable: the fact that one needs to copy the children in case the model doesn’t return a Set is one thing that worries me a bit. The fact that one will need to copy them specifically into a LinkedHashSet (or similar Set implementation) in order to preserve the iteration order of the model, is another thing. Uses cases where the order is important: DOM traversal, UI component hierarchy, …

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is PECS (Producer Extends Consumer Super)?
This principle states that: Use an extends wildcard when you only get values out of a structure. Use a super wildcard when you...
Read more >
Java Generics PECS - Producer Extends Consumer Super
Yesterday, I was going through some java collection APIs and I found two methods primarily used for adding elements into a collection.
Read more >
Java: Producer Extends, Consumer Super - Medium
It is easy to remember which wildcard to use at which occasion using the word acronym PECS, Producer Extends, Consumer Super.
Read more >
Java Generics PECS – Producer Extends Consumer Super
In this article, we'll explore the usage of Java Generics when it comes to producing and consuming collections.
Read more >
Generics PECS - [OOP & Java #11] - DEV Community ‍ ‍
We will use the following code for ease of reference and discussion. // assignment List<? exten... Tagged with computerscience, java.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found