0

Could someone help me to fix this compilation error, please?

I 'd like to define a map method in the generic interface called Suite and use it like this:

Suite < Integer > suite2 = Suite.from("34", "78", "23").map(Integer::parseInt);

assertEquals(3, suite.size());
assertEquals(34, (int)suite.get(0));
assertEquals(78, (int)suite.get(1));
assertEquals(23, (int)suite.get(2));

The the call to the same method with a function and parameter compile well:

Suite<Integer> suite1 = Suite.from(1, 2).map(x -> x * 2);

assertEquals(2, suite.size());
assertEquals(2, (int)suite.get(0));
assertEquals(4, (int)suite.get(1));

So I've defined the method in the interface like this

public interface Suite<E> {

    public <E> Suite<E> map(int i, Function<? super E, ? extends E> f);
}

Note: this is almost the same protytype as the map method of Stream class (except the paramter i )

My problem is in my test, this line does not compile:

map(Integer::parseInt)

because of these errors:

  • The type Integer does not define toString(Object) that is applicable here.
  • Type mismatch: cannot convert Suite<Object> to Suite<String>

I'm tried to redefine the function with a Supplier<E> but it does not work.

11
  • Your code doesn't make sense. What kind of parameter do you expect to pass to the function? E or not E? Commented Jan 10, 2018 at 20:36
  • just spitballing here, what if you used .span(1, x -> x.toString()) Commented Jan 10, 2018 at 20:37
  • THis might be your problem. Function<? super E, ? extends E> Commented Jan 10, 2018 at 20:37
  • try to define 2 generic classes public <E, F> Suite<F> map(int i, Function<? super E, ? extends F> ; Commented Jan 10, 2018 at 21:19
  • Please see my updated post with all the use case test. Commented Jan 10, 2018 at 21:22

2 Answers 2

2
 Function<? super E, ? extends E> function

Integer is not a super class of String, hence this fails:

Suite <String> suite1 = Suite.span(1, Integer::toString);

reference: Difference between <? super T> and <? extends T> in Java

Sign up to request clarification or add additional context in comments.

2 Comments

In fact, this method must be able used in the same way the map of the Stream class. (Please my post updated). And I used the same prototype. The test case is the specification so I think it must work
@Toto Your is not at all the same. Stream.map() has two generic types: T (the input type, defined at the class level) and R (the return type).
2

Usually functional mapping expects type changing, e.g. argument and result types are diverged:

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#map-java.util.function.Function-

Suite < Integer > suite2 = Suite.from("34", "78", "23").map(Integer::parseInt);

Here you change String type to Integer, thus you can't use common E generic name like

public <E> Suite<E> map(int i, Function<? super E, ? extends E> f);

but:

public <T, R> Suite<R> map(int i, Function<? super T, ? extends R> f);

or:

public interface Suite<E> {
    public <R> Suite<R> map(int i, Function<? super E, ? extends R> f);
}

7 Comments

radistao, juste tried your idea . So I added the second type R : public <R> Suite<R> map(int i, Function<? super E, ? extends R> f); but many lines won't compile. Example: Suite.from("abc", "def", "ghi") .map(String::length)
Radistao, other error: this line does not compile any more, after adding a return type R in the prototype : Suite<Integer> suite = Suite.from(1, 2).map(x -> x * 2);
could you show whole Suite class? It is not clear what exactly should be done
as i mentioned in another commend above: you methods arguments list are mismatched
remove int i, from map function
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.