Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Of course, everyone thinks to understand lambdas and generics. I was one of those until recently. Look at my code: package debug; import java.util.function.Function; public class Understand...
#2: Post edited
- Of course, everyone *thinks* to understand lambdas and generics. I was one of those until recently.
- Look at my code:
>- package debug;
- import java.util.function.Function;
- public class UnderstandingGenerix {
- public static void main(String[] args) {
- {
- Function<?, ?> code = (String O) -> {
- System.out.println("todo" + O);
- return Double.valueOf(1.5);
- };
- // Object x = code.apply("xxx"); // compile error
- // Object x = code.apply((Object) "xxx"); // compile error
- // Double x = code.apply("xxx"); // compile error
- }
- {
- Function<Object, Object> code = (O) -> {
- System.out.println("todo" + O);
- return Double.valueOf(1.5);
- };
- Object x = code.apply("xxx");
- x = code.apply((Object) "xxx");
- Double xx = (Double) code.apply("xxx");
- }
- {
- Function code = (O) -> {
- System.out.println("todo" + O);
- return 1.5;
- };
- Object x = code.apply("xxx");
- x = code.apply((Object) "xxx");
- Double xx = (Double) code.apply("xxx");
- }
- }
- }
(Ooops, why the dangling brace?)- From the net, the question mark is a "wildcard" matching *everything* (except primitive types). So "?" and "Object" should be equivalent.
- What part did I miss?
- Of course, everyone *thinks* to understand lambdas and generics. I was one of those until recently.
- Look at my code:
- ```java
- package debug;
- import java.util.function.Function;
- public class UnderstandingGenerix {
- public static void main(String[] args) {
- {
- Function<?, ?> code = (String O) -> {
- System.out.println("todo" + O);
- return Double.valueOf(1.5);
- };
- // Object x = code.apply("xxx"); // compile error
- // Object x = code.apply((Object) "xxx"); // compile error
- // Double x = code.apply("xxx"); // compile error
- }
- {
- Function<Object, Object> code = (O) -> {
- System.out.println("todo" + O);
- return Double.valueOf(1.5);
- };
- Object x = code.apply("xxx");
- x = code.apply((Object) "xxx");
- Double xx = (Double) code.apply("xxx");
- }
- {
- Function code = (O) -> {
- System.out.println("todo" + O);
- return 1.5;
- };
- Object x = code.apply("xxx");
- x = code.apply((Object) "xxx");
- Double xx = (Double) code.apply("xxx");
- }
- }
- }
- ```
- From the net, the question mark is a "wildcard" matching *everything* (except primitive types). So "?" and "Object" should be equivalent.
- What part did I miss?
#1: Initial revision
Trouble understanding java lambdas and generics
Of course, everyone *thinks* to understand lambdas and generics. I was one of those until recently.
Look at my code:
>
package debug;
import java.util.function.Function;
public class UnderstandingGenerix {
public static void main(String[] args) {
{
Function<?, ?> code = (String O) -> {
System.out.println("todo" + O);
return Double.valueOf(1.5);
};
// Object x = code.apply("xxx"); // compile error
// Object x = code.apply((Object) "xxx"); // compile error
// Double x = code.apply("xxx"); // compile error
}
{
Function<Object, Object> code = (O) -> {
System.out.println("todo" + O);
return Double.valueOf(1.5);
};
Object x = code.apply("xxx");
x = code.apply((Object) "xxx");
Double xx = (Double) code.apply("xxx");
}
{
Function code = (O) -> {
System.out.println("todo" + O);
return 1.5;
};
Object x = code.apply("xxx");
x = code.apply((Object) "xxx");
Double xx = (Double) code.apply("xxx");
}
}
}
(Ooops, why the dangling brace?)
From the net, the question mark is a "wildcard" matching *everything* (except primitive types). So "?" and "Object" should be equivalent.
What part did I miss?
