logo
down
shadow

Inferring parameter types in a generic class which are nested generic types of provided arguments


Inferring parameter types in a generic class which are nested generic types of provided arguments

Content Index :

Inferring parameter types in a generic class which are nested generic types of provided arguments
Tag : java , By : Sid
Date : November 24 2020, 01:01 AM

it fixes the issue A thought about it a while but I didn’t find any simple generic construct allowing this kind of type inference.
Regarding the specialized dummy-argument constructor it’s worth noting that you don’t need to pass an actual Class instance; any type which allows inferring G, including G itself would work. E.g., if you change the public MyClass(Class gClass) {} constructor to public MyClass(G ignored) {} you can instantiate the class using new MyClass<>((ConcreteOtherClass)null).
class MyClass<F extends Field, G extends OtherClass<F>> {

    public MyClass() {}
    public MyClass(G dummy) {}

    public static <T> T infer() {
        return null;
    }
}
new MyClass<>(MyClass.<ConcreteOtherClass>infer())
interface Instantiator<T extends OtherClass<?>> extends Runnable {
    default <M extends MyClass<?,? extends T>> M get(Supplier<? extends M> s) {
        return s.get();
    }
    static <X extends OtherClass<?>> Instantiator<X> i() {
        return ()->{};
    }
}
Instantiator.<ConcreteOtherClass>i().get(MyClass::new)

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Inferring generic types with functional composition


Tag : chash , By : ralph okochu
Date : March 29 2020, 07:55 AM
will help you Suppose I want to implement a functional composition, like this: , The following code would work:
Func<string, string> toUpper = ToUpper;
Func<string, string> replicate = Replicate;

// now the compiler knows that the parameters are Func<string, string>
var h = Compose(toUpper, replicate);

Inferring generic types of nested static generic functions


Tag : java , By : Ben
Date : March 29 2020, 07:55 AM
this will help Type inference is nasty and complicated. They have to stop somewhere. Consider
static <T> T foo();

String s = foo();

print( foo() )

Inferring 2 out of 3 generic types


Tag : chash , By : stu73
Date : March 29 2020, 07:55 AM
I wish did fix the issue.
Is there a way to force the method to infer 2 out of 3 generic parameters?
public static AttributeFetcher<T, TProperty> FetchFrom<T, TProperty>
    (this T instance, Expression<Func<T, TProperty>> propertySelector)
{
    return new AttributeFetcher<T, TProperty>(instance, propertySelector);     
}

public class AttributeFetcher<T, TProperty>
{
    private readonly T instance;
    private readonly Expression<Func<T, TProperty>> propertySelector;

    // Add obvious constructor

    public U Attribute<U>() where U : Attribute
    {
        // Code as before
    }
}
cc.FetchFrom(x => x.Name).Attribute<NullableAttribte>();

Why can't I instantiate a generic class inferring types from anonymous objects?


Tag : chash , By : Steve O.
Date : March 29 2020, 07:55 AM
To fix this issue You can't put together generics, anonymous types and type inference in constructors. The compiler just won't get it. You can create a workaround through a helper class such as:
public static class InvalidResponseExceptionHelper
{
    public static InvalidResponseException<TReq, TResp> Create<TReq, TResp>
        (string message, TReq requestData, TResp responseData)
    {
        return new InvalidResponseException<TReq, TResp>(message, 
            requestData, responseData);
    }
}

Inferring generic parameters from generic types


Tag : typescript , By : jsmb
Date : March 29 2020, 07:55 AM
will be helpful for those in need I don't know enough about how autocomplete works to tell you exactly why it doesn't work in your case; my intuition is that it hasn't fully committed to the generic type inference. One workaround that forces it to commit to the inferred type is to change set into a curried function like this:
const intelliSet = <B extends Box<{}>>(box: B) => 
  (newValue: B['value']) => set(box, newValue);

intelliSet(bbox)( { foo: 'bar' }); // note the second function call
intelliSet(bbox)( { /* autocomplete works here */ });
Related Posts Related QUESTIONS :
  • JUnit vs TestNG
  • Are there any decent free Java data plotting libraries out there?
  • Java Time Zone is messed up
  • Eclipse on win64
  • Automate builds for Java RCP for deployment with JNLP
  • Is there a real benefit of using J#?
  • Why can't I use a try block around my super() call?
  • Accessing post variables using Java Servlets
  • Is this really widening vs autoboxing?
  • How can I Java webstart multiple, dependent, native libraries?
  • Why doesn't Java autoboxing extend to method invocations of methods of the autoboxed types?
  • How do I use Java to read from a file that is actively being written to?
  • What code analysis tools do you use for your Java projects?
  • IllegalArgumentException or NullPointerException for a null parameter?
  • How do I configure and communicate with a serial port?
  • What are the different methods to parse strings in Java?
  • Android Broadcastreceiver for other apps install/delete not working
  • Android Studio onClick not working in BindViewHolder
  • How to use Spring Converter for some controllers only?
  • How verify that 3 numbers in sequence are equals?
  • When using .compareTo to compare dates, why doesn't it take Months into account?
  • Does the perfomance of "filter then map" and "map then filter" differ in a Stream?
  • How can I set the initial Delay after pressing the start Button to a specific time (HH:mm:ss) format
  • How to switch between Android devices during the tests
  • How to configure java.util.logging via properties to use standard output?
  • How to iterate through array in order
  • Is there better way of iteration to find the evenly divisible number?
  • How do I avoid using if statements with a large amount of variables in java
  • Writing JUnit test cases for a Spring Boot Service Application with autowired components
  • Cors for GET with Postman not showing headers
  • Printing values in different column same row using APACHE POI
  • Fully decompile java6 web application
  • Passing keycloak configuration parameters in the code rather than reading from application.properties
  • setDataSource() IO exception
  • Unexpected Output while retrieving Data from mongodb and displaying in a csv file?
  • Algorithm for searching a value in two arrays
  • How to avoid casting with generic return values?
  • Java/RegEx - Negation of pattern not working
  • How to split a string to non empty words if it might include a separator like tab on first place
  • Supplier<Sequence<String>> cannot be iterated more than once
  • Why there is only one thread can actually started in @PostConstruct method?
  • Manage CompletionStage inside of Netty handler
  • Url Problem while Developing on Localhost and deploy on Remote Virtual Server
  • How to identify the missing type id in Jackson error?
  • android data binding error: cannot find symbol
  • Spring Boot application with a jar dependency does not run after maven build
  • Spring Data JPA query , filter ? search engine ? JPQL?
  • Why LiveData returns null in ViewModel?
  • what this line of code mean....new URLClassLoader(new URL[0],getClass().getClassLoader());
  • Why do need to use new Random() instead of just Random Randomnum?
  • I want to access zk components from the java file
  • How do I cast FieldValue.serverTimestamp() to Kotlin/Java Date Class
  • Insertion Sort Double Array with User Input - JAVA
  • Creating 2 dimesional array with user input and find sum of specific columns
  • can not get Advertising ID Provider in android
  • Convert list of Objects to map of properties
  • How to represent an undirected weighted graph in java
  • Return values as array from collection
  • ByteBuddy generic method return cast to concrete type
  • ImageView hides the round corners of the parent
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com