I want to do something like
public interface Foo<R, P...> {
public R bar(P...) {/*misc*/}
}
to get an array of types to use in my bound implementation. Is this possible in java?
Varargs is designed to let me have any number of arguments of a given class.
I want to use it (or something similar) to have my method accept several arguments, each of which is a member of a given different class. These classes are defined when the generic is bound.
I aware there are work arounds, but is there a type-safe way to do this?
The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.
A Generic class can have muliple type parameters.
The generic value variable is a type of variable with a wide range that can store any kind of data, including text, numbers, dates and arrays, and is particular to UiPath Studio. Generic value variables are automatically converted to other types, in order to perform certain actions.
Creating a simple generic type is straightforward. First, declare your type variables by enclosing a comma-separated list of their names within angle brackets after the name of the class or interface. You can use those type variables anywhere a type is required in any instance fields or methods of the class.
Since you apparently want to be able to make bar
take multiple parameters of different types (something varargs are not used for), I'd suggest making it instead take a single parameter. You can then make that single parameter be a container that holds each of the individual "parameters" you want to use.
In general, it would be best to make a class for each set of parameters you want to use with this method so that you have good names for each of the objects it contains. However, you could also do something like creating a series of tuple types (such as Pair
) to use as holders.
Here's an example:
public class Foo<R, P> {
/*
* Not sure how you intend to provide any kind of implementation
* here since you don't know what R or P are.
*/
public R bar(P parameters) { ... }
}
public class SomeFoo extends Foo<SomeResult, Pair<Baz, Bar>> {
public SomeResult bar(Pair<Baz, Bar> parameters) { ... }
}
SomeFoo foo = ...
SomeResult result = foo.bar(Pair.of(baz, bar));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With