Java 8 - Wildcard extends with BiPredicate not working [duplicate]
This question already has an answer here:
Difference between <? super T> and <? extends T> in Java [duplicate]
15 answers
I have no idea why it's not working.
Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)
import java.util.function.BiPredicate;
public class PredTest {
public static void main(String args) {
class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};
class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};
BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();
// ########### I see error in the below line
System.out.println(tester.test(f, m));
}
}
java generics java-8
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Difference between <? super T> and <? extends T> in Java [duplicate]
15 answers
I have no idea why it's not working.
Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)
import java.util.function.BiPredicate;
public class PredTest {
public static void main(String args) {
class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};
class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};
BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();
// ########### I see error in the below line
System.out.println(tester.test(f, m));
}
}
java generics java-8
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You should use? super Fruitinstead. What is the difference between <? super T> and <? extends T>?
– BackSlash
Nov 22 at 10:33
PECSstrikes again...
– Eugene
Nov 22 at 10:33
1
@BackSlash or indeed justBiPredicate<Fruit, Fruit>.
– Michael
Nov 22 at 10:45
add a comment |
This question already has an answer here:
Difference between <? super T> and <? extends T> in Java [duplicate]
15 answers
I have no idea why it's not working.
Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)
import java.util.function.BiPredicate;
public class PredTest {
public static void main(String args) {
class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};
class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};
BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();
// ########### I see error in the below line
System.out.println(tester.test(f, m));
}
}
java generics java-8
This question already has an answer here:
Difference between <? super T> and <? extends T> in Java [duplicate]
15 answers
I have no idea why it's not working.
Error message in eclipse:
The method test(Fruit, capture#1-of ? extends Fruit) in the type BiPredicate is not applicable for the arguments (Fruit, Mango)
import java.util.function.BiPredicate;
public class PredTest {
public static void main(String args) {
class Fruit {
public String name;
public String color;
Fruit(String name) {this.name = name; }
};
class Apple extends Fruit {
Apple() {super("Apple");}
};
class Mango extends Fruit {
Mango() {super("Mango");}
};
BiPredicate<Fruit, ? extends Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
Fruit f = new Fruit("Not named");
Apple a = new Apple();
Mango m = new Mango();
// ########### I see error in the below line
System.out.println(tester.test(f, m));
}
}
This question already has an answer here:
Difference between <? super T> and <? extends T> in Java [duplicate]
15 answers
java generics java-8
java generics java-8
asked Nov 22 at 10:30
Ajeetkumar
4642719
4642719
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Michael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 10:45
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
You should use? super Fruitinstead. What is the difference between <? super T> and <? extends T>?
– BackSlash
Nov 22 at 10:33
PECSstrikes again...
– Eugene
Nov 22 at 10:33
1
@BackSlash or indeed justBiPredicate<Fruit, Fruit>.
– Michael
Nov 22 at 10:45
add a comment |
You should use? super Fruitinstead. What is the difference between <? super T> and <? extends T>?
– BackSlash
Nov 22 at 10:33
PECSstrikes again...
– Eugene
Nov 22 at 10:33
1
@BackSlash or indeed justBiPredicate<Fruit, Fruit>.
– Michael
Nov 22 at 10:45
You should use
? super Fruit instead. What is the difference between <? super T> and <? extends T>?– BackSlash
Nov 22 at 10:33
You should use
? super Fruit instead. What is the difference between <? super T> and <? extends T>?– BackSlash
Nov 22 at 10:33
PECS strikes again...– Eugene
Nov 22 at 10:33
PECS strikes again...– Eugene
Nov 22 at 10:33
1
1
@BackSlash or indeed just
BiPredicate<Fruit, Fruit>.– Michael
Nov 22 at 10:45
@BackSlash or indeed just
BiPredicate<Fruit, Fruit>.– Michael
Nov 22 at 10:45
add a comment |
1 Answer
1
active
oldest
votes
Suppose you changed your lambda expression to:
BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};
Would you still expect the compiler to allow passing a Mango to this BiPredicate?
Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.
Changing your BiPredicate to:
BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
will eliminate the compilation error.
But how can it then accessnf.name, ifnfcould be any superclass ofFruit(egObject)?
– daniu
Nov 22 at 10:41
1
@daniuBiPredicate<Fruit, ? super Fruit>could be assigned aBiPredicate<Fruit, Fruit>, so you can't pass anObjecttotester.test().
– Eran
Nov 22 at 10:50
ButBiPredicate<Fruit, ? super Fruit>could also be assigned aBiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12
1
@daniu yes, but then the lambda expression won't containnf.name. And anyway, the compiler can't allow you to passObjectas the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to theBiPredicate<Fruit, ? super Fruit>variable.
– Eran
Nov 22 at 13:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Suppose you changed your lambda expression to:
BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};
Would you still expect the compiler to allow passing a Mango to this BiPredicate?
Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.
Changing your BiPredicate to:
BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
will eliminate the compilation error.
But how can it then accessnf.name, ifnfcould be any superclass ofFruit(egObject)?
– daniu
Nov 22 at 10:41
1
@daniuBiPredicate<Fruit, ? super Fruit>could be assigned aBiPredicate<Fruit, Fruit>, so you can't pass anObjecttotester.test().
– Eran
Nov 22 at 10:50
ButBiPredicate<Fruit, ? super Fruit>could also be assigned aBiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12
1
@daniu yes, but then the lambda expression won't containnf.name. And anyway, the compiler can't allow you to passObjectas the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to theBiPredicate<Fruit, ? super Fruit>variable.
– Eran
Nov 22 at 13:18
add a comment |
Suppose you changed your lambda expression to:
BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};
Would you still expect the compiler to allow passing a Mango to this BiPredicate?
Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.
Changing your BiPredicate to:
BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
will eliminate the compilation error.
But how can it then accessnf.name, ifnfcould be any superclass ofFruit(egObject)?
– daniu
Nov 22 at 10:41
1
@daniuBiPredicate<Fruit, ? super Fruit>could be assigned aBiPredicate<Fruit, Fruit>, so you can't pass anObjecttotester.test().
– Eran
Nov 22 at 10:50
ButBiPredicate<Fruit, ? super Fruit>could also be assigned aBiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12
1
@daniu yes, but then the lambda expression won't containnf.name. And anyway, the compiler can't allow you to passObjectas the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to theBiPredicate<Fruit, ? super Fruit>variable.
– Eran
Nov 22 at 13:18
add a comment |
Suppose you changed your lambda expression to:
BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};
Would you still expect the compiler to allow passing a Mango to this BiPredicate?
Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.
Changing your BiPredicate to:
BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
will eliminate the compilation error.
Suppose you changed your lambda expression to:
BiPredicate<Fruit, ? extends Fruit> tester = (Fruit f, Apple nf) -> {
System.out.println(nf.name);
return true;
};
Would you still expect the compiler to allow passing a Mango to this BiPredicate?
Based on the compile time type - BiPredicate<Fruit, ? extends Fruit> - of tester, the compiler doesn't know if Mango is allowed, so it doesn't allow it.
Changing your BiPredicate to:
BiPredicate<Fruit, ? super Fruit> tester = (f, nf) -> {
System.out.println(nf.name);
return true;
};
will eliminate the compilation error.
answered Nov 22 at 10:37
Eran
278k37447533
278k37447533
But how can it then accessnf.name, ifnfcould be any superclass ofFruit(egObject)?
– daniu
Nov 22 at 10:41
1
@daniuBiPredicate<Fruit, ? super Fruit>could be assigned aBiPredicate<Fruit, Fruit>, so you can't pass anObjecttotester.test().
– Eran
Nov 22 at 10:50
ButBiPredicate<Fruit, ? super Fruit>could also be assigned aBiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12
1
@daniu yes, but then the lambda expression won't containnf.name. And anyway, the compiler can't allow you to passObjectas the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to theBiPredicate<Fruit, ? super Fruit>variable.
– Eran
Nov 22 at 13:18
add a comment |
But how can it then accessnf.name, ifnfcould be any superclass ofFruit(egObject)?
– daniu
Nov 22 at 10:41
1
@daniuBiPredicate<Fruit, ? super Fruit>could be assigned aBiPredicate<Fruit, Fruit>, so you can't pass anObjecttotester.test().
– Eran
Nov 22 at 10:50
ButBiPredicate<Fruit, ? super Fruit>could also be assigned aBiPredicate<Fruit, Object>, no?
– daniu
Nov 22 at 13:12
1
@daniu yes, but then the lambda expression won't containnf.name. And anyway, the compiler can't allow you to passObjectas the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to theBiPredicate<Fruit, ? super Fruit>variable.
– Eran
Nov 22 at 13:18
But how can it then access
nf.name, if nf could be any superclass of Fruit (eg Object)?– daniu
Nov 22 at 10:41
But how can it then access
nf.name, if nf could be any superclass of Fruit (eg Object)?– daniu
Nov 22 at 10:41
1
1
@daniu
BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().– Eran
Nov 22 at 10:50
@daniu
BiPredicate<Fruit, ? super Fruit> could be assigned a BiPredicate<Fruit, Fruit>, so you can't pass an Object to tester.test().– Eran
Nov 22 at 10:50
But
BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?– daniu
Nov 22 at 13:12
But
BiPredicate<Fruit, ? super Fruit> could also be assigned a BiPredicate<Fruit, Object>, no?– daniu
Nov 22 at 13:12
1
1
@daniu yes, but then the lambda expression won't contain
nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.– Eran
Nov 22 at 13:18
@daniu yes, but then the lambda expression won't contain
nf.name. And anyway, the compiler can't allow you to pass Object as the second parameter of the BiPredicate's method, since your code must be type safe for any possible assignment to the BiPredicate<Fruit, ? super Fruit> variable.– Eran
Nov 22 at 13:18
add a comment |
You should use
? super Fruitinstead. What is the difference between <? super T> and <? extends T>?– BackSlash
Nov 22 at 10:33
PECSstrikes again...– Eugene
Nov 22 at 10:33
1
@BackSlash or indeed just
BiPredicate<Fruit, Fruit>.– Michael
Nov 22 at 10:45