Honestly, I’m not even sure whether that title makes sense. Hopefully the code following will explain the issue at hand. My issue is with the two methods applySimpleBound() and applyBoundWithGenericType(). The former is compiling fine, but needs explicit casting, which is what I’d like to get rid of. The later does not compile, because .apply(this) requires a type SELF but
Tag: generics
How to define a general interface for several type-specific classes?
While trying to program to interfaces, I regularly find myself in the following situation: I have several very similar classes representing containers or algorithms for different types. I would like to define a common interface for these classes. Consider, e.g., a string container. Such a container will likely have string processing methods. Since those methods are easily represented using generic
Unable to add value to a java generic list
Here’s my sample code: I think I should be able to add integer to items, but compilation fails: Anyone knows what’s wrong here? Answer Let us consider a more complete version of your example: Suppose for the sake of argument that the compiler says that is OK. And now we will create an instance and call the func method: Here
Java Method that returns different types of generic Lists
I’m currently trying to write a method that goes through a list of Ant-Objects and returns a list of AntScouts, that extend Ant. In general, List<Ant> can contain a lot of different Objects that inherit from Ant. I also have an enum for the different kinds of ants: This enum causes a warning: And this is the method that currently
Wildcard generic type of predicate in stream not applicable
I have a trimmed down version of my code to illustrate the issue. Here’s the compilation error I’m getting: My code: How would you fix the code to remove the error? Thank you! Answer The fact that you have applied instanceof check in the filter() doesn’t change the type of the stream, it remains to be Stream<Object>. filter() operation is
Passing function as argument to a Function, and using it on Generic object
I would like to do something like below. A and B class will be my generic type as <T>. Example for a getter function for class A and B. I want to pass a getterFunction as argument to my sliceGenericData() and I would like to use it on type <T> (Class A or B). Since those classes have different name
Not able to create generic bounded class objects with interface
I am trying to use bounded types with generics to create generic objects of subclasses (these implement an interface). But I am getting type mismatch errors when initializing objects with the subclasses. Here is the interface: Here’s the class that implements this interface: Now I created 2 classes (SegmentPageScanResult and ItemProcessor) with the bounded generic type as: and When I
How to merge methods with unrelated parameter types into one generic one?
For java/C# There is my code: Can I merge AddTable and AddCup into one method like Add<T> like this: The problem is that there are too many if else. Do you have any better suggestions? Answer Name the methods the same so that they will be overloads of each other: The compiler is smart enough to call the correct method.
Generic type get method doesn’t work in a foreach loop – java: incompatible types: java.lang.Object cannot be converted to Error
I am studying Generics in Java in Oracle docs. In Type Inference & Generic Methods section there is this code and it is not working in Intellij Idea. This is Box class This is BoxDemo class. Here is Screen Answer Try to change your for and iterate from boxes the type Box box instead of Box box
How rewrite Java generec’s into Kotlin?
I have java class and I need to rewrite it in kotlin. But I can’t rewrite generics into it. Java class: My Kotlin class Sign (1) I marked the my problem area by my opinion. But maybe the problem is somewhere else. Answer Kotlin’s <out FlagFilter> is not equivalent to Java’s <T extends FlagFilter>. Using out at a generic declaration