Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploit generic variance to ensure typing works as best as possible #1533

Closed
victornoel opened this issue Jan 16, 2021 · 16 comments
Closed

Exploit generic variance to ensure typing works as best as possible #1533

victornoel opened this issue Jan 16, 2021 · 16 comments

Comments

@victornoel
Copy link
Collaborator

Lots of constructors and methods in cactoos take Iterable, Scalar, Func and so on.

Because of the way typing works in Java, it can sometimes be challenging to have code compile even though it is conceptually sound. For example if we have a method or a constructor like this:

void method(Iterable<Scalar<Number>> numbers) { ... }

Then the following happens;

List<LengthOf> list = new ListOf<>(new LengthOf("some text"));
method(list); // does not compile because the types does not exactly match
method(new ListOf<>(new LengthOf("some text")); // compile because the correct type is inferred

In more complex situation, type inference does not even succeed.

The typical solution is to ensure that when using generics as parameters, then variance should be properly defined. The basic rules is that if a generic type is used in outputs of the interface methods, it should be relaxed w.r.t to its subclasses and if it is an input, then w.r.t. its parent classes. For, the above example, this gives us:

void method(Iterable<? extends Scalar<? extends Number>> numbers) { ... }

Of course this only makes sense if the type parameter is either a non-final class or a generic type from the enclosing class or method.

So every where Iterable, Iterator, Scalar, Callable, Supplier, Func, Proc, BiFunc, BiProc are used we need:

  • Iterable<? extends X>
  • Iterator<? extends X>
  • Scalar<? extends X>
  • Supplier<? extends X>
  • Callable<? extends X>
  • Func<? super X, ? extends Y>
  • Proc<? super X>
  • BiFunc<? super X1, ? super X2, ? extends Y>
  • BiProc<? super X1, ? extends X2>

Let's start by package bytes and go on through all of them one by one in alphabetical order by introducing todos once a package or part of it is done. Let's also add some more tests when it makes sense to validate it compiles correctly.

@victornoel
Copy link
Collaborator Author

@0crat in

@victornoel victornoel added this to the 1.0 milestone Jan 16, 2021
victornoel added a commit to victornoel/cactoos that referenced this issue Jan 16, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 8, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 8, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 9, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 13, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 13, 2021
baudoliver7 pushed a commit to baudoliver7/cactoos that referenced this issue Mar 13, 2021
@0pdd
Copy link
Collaborator

0pdd commented Mar 14, 2021

@victornoel 13 puzzles #1563, #1564, #1565, #1566, #1567, #1568, #1569, #1570, #1571, #1572, #1573, #1574, #1575 are still not solved.

@victornoel
Copy link
Collaborator Author

@baudoliver7 thx

@0crat 0crat added qa and removed 0crat/scope labels Mar 14, 2021
@0crat
Copy link
Collaborator

0crat commented Mar 14, 2021

@sereshqua/z please review this job completed by @baudoliver7/z, as in §30; the job will be fully closed and all payments will be made when the quality review is completed

@sereshqua
Copy link

@0crat quality good

@0pdd
Copy link
Collaborator

0pdd commented May 13, 2021

@victornoel 13 puzzles #1563, #1564, #1565, #1566, #1567, #1568, #1569, #1570, #1571, #1573, #1574, #1575, #1613 are still not solved; solved: #1572.

@0pdd
Copy link
Collaborator

0pdd commented May 13, 2021

@victornoel 13 puzzles #1563, #1564, #1565, #1566, #1568, #1569, #1570, #1571, #1573, #1574, #1575, #1613, #1614 are still not solved; solved: #1567, #1572.

@0pdd
Copy link
Collaborator

0pdd commented Jun 22, 2021

@victornoel 12 puzzles #1563, #1564, #1566, #1568, #1569, #1570, #1571, #1573, #1574, #1575, #1613, #1614 are still not solved; solved: #1565, #1567, #1572.

@0pdd
Copy link
Collaborator

0pdd commented Sep 5, 2021

@victornoel 11 puzzles #1563, #1564, #1566, #1568, #1569, #1570, #1571, #1573, #1575, #1613, #1614 are still not solved; solved: #1565, #1567, #1572, #1574.

@0pdd
Copy link
Collaborator

0pdd commented Sep 26, 2021

@victornoel 10 puzzles #1563, #1564, #1566, #1568, #1569, #1570, #1571, #1573, #1613, #1614 are still not solved; solved: #1565, #1567, #1572, #1574, #1575.

@0pdd
Copy link
Collaborator

0pdd commented Sep 27, 2021

@victornoel 9 puzzles #1563, #1564, #1566, #1569, #1570, #1571, #1573, #1613, #1614 are still not solved; solved: #1565, #1567, #1568, #1572, #1574, #1575.

@0pdd
Copy link
Collaborator

0pdd commented Oct 2, 2021

@victornoel 8 puzzles #1563, #1564, #1566, #1569, #1570, #1571, #1613, #1614 are still not solved; solved: #1565, #1567, #1568, #1572, #1573, #1574, #1575.

@0pdd
Copy link
Collaborator

0pdd commented Oct 16, 2021

@victornoel 8 puzzles #1563, #1564, #1566, #1570, #1571, #1613, #1614, #1630 are still not solved; solved: #1565, #1567, #1568, #1569, #1572, #1573, #1574, #1575.

@0pdd
Copy link
Collaborator

0pdd commented Jul 11, 2022

@victornoel 5 puzzles #unknown, #unknown, #unknown, #unknown, #unknown are still not solved.

@0pdd
Copy link
Collaborator

0pdd commented Jul 20, 2022

@victornoel 4 puzzles #unknown, #unknown, #unknown, #unknown are still not solved.

@0pdd
Copy link
Collaborator

0pdd commented Jul 30, 2022

@victornoel 7 puzzles #1563, #1564, #1570, #1571, #1613, #1614, #1630 are still not solved; solved: #1565, #1566, #1567, #1568, #1569, #1572, #1573, #1574, #1575.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants