Does it matter if function returns T or object if T can not be inferred from context?

Eitanos30

I found the following source code in gson:

public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException
{
    JsonReader jsonReader = newJsonReader(json);
    T object = (T) fromJson(jsonReader, typeOfT);
    assertFullConsumption(object, jsonReader);
    return object;
}

Does it matter if fromJson function declares to return an Object or return T? at least from my knowledge, if the T can not be inferred by function arguments it acts exactly as object. So why the source code uses T instead of an Object?

xehpuk

This doesn't return Object but a type that's either inferred from the context or – if that's not possible – by passing a type parameter with a type witness: YourClass.<String>fromJson()

Note that this won't magically work. If the object that's returned from the internal call isn't compatible with T at runtime, the assignment (of the outer return value) will throw a ClassCastException.

Example:

public class MyClass {
    public static void main(String args[]) {
        String result1 = MyClass.test(); // works as expected
        Object result2 = MyClass.<Integer>test(); // works "surprisingly"
        int result3 = MyClass.test(); // cannot be cast to java.lang.Integer
    }
    
    static <T> T test() {
        try {
            return (T) "Hello World";
        } catch (ClassCastException e) {
            throw new Error(); // never reached
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't inherit from context object?

From Dev

Can't return a temporary object from function

From Dev

type argument from Action<T> cannot be inferred, but from Func<T> can be

From Dev

Does T matter when passing IEnumerable<T>?

From Dev

Why can't the types be inferred for nested functions

From Dev

c++ can't access object pointer from 'this' in callback function

From Dev

Can't access a class object from a function says unknown class

From Dev

Can't access asp:TextBox tag from the C# file : the name 'txtUsername' does not exist in the current context

From Dev

Does it matter if a function returns int to a function returning BOOL on Windows?

From Dev

Can't use function return value in write context - next() function

From Dev

Can't install GRUB, no matter what

From Dev

Iterator<T> returns Object

From Dev

Iterator<T> returns Object

From Dev

React Error (TypeError): Can't add property context, object is not extensible

From Dev

Building up inferred object works until it doesn't

From Dev

Can't open Object Arraylist with fileio function

From Dev

Why can't I instantiate an object in a function

From Dev

Can't use function in object using this

From Dev

Can't use function return value in write context Laravel 4

From Dev

Can't access V8 Context in "callback" function

From Dev

Can't use function return value in write context error message

From Dev

Fatal error: Can't use function return value in write context in /

From Dev

Fatal error: Can't use function return value in write context

From Dev

Can't use function return value in write context php script

From Dev

Can't access V8 Context in "callback" function

From Dev

Writing a function that returns => T

From Dev

Why does my function only returns the first object from the array

From Dev

Why can't the typeclass constraint be inferred and get ambiguous instead?

From Dev

jQuery function won't run when removed from context

Related Related

  1. 1

    Can't inherit from context object?

  2. 2

    Can't return a temporary object from function

  3. 3

    type argument from Action<T> cannot be inferred, but from Func<T> can be

  4. 4

    Does T matter when passing IEnumerable<T>?

  5. 5

    Why can't the types be inferred for nested functions

  6. 6

    c++ can't access object pointer from 'this' in callback function

  7. 7

    Can't access a class object from a function says unknown class

  8. 8

    Can't access asp:TextBox tag from the C# file : the name 'txtUsername' does not exist in the current context

  9. 9

    Does it matter if a function returns int to a function returning BOOL on Windows?

  10. 10

    Can't use function return value in write context - next() function

  11. 11

    Can't install GRUB, no matter what

  12. 12

    Iterator<T> returns Object

  13. 13

    Iterator<T> returns Object

  14. 14

    React Error (TypeError): Can't add property context, object is not extensible

  15. 15

    Building up inferred object works until it doesn't

  16. 16

    Can't open Object Arraylist with fileio function

  17. 17

    Why can't I instantiate an object in a function

  18. 18

    Can't use function in object using this

  19. 19

    Can't use function return value in write context Laravel 4

  20. 20

    Can't access V8 Context in "callback" function

  21. 21

    Can't use function return value in write context error message

  22. 22

    Fatal error: Can't use function return value in write context in /

  23. 23

    Fatal error: Can't use function return value in write context

  24. 24

    Can't use function return value in write context php script

  25. 25

    Can't access V8 Context in "callback" function

  26. 26

    Writing a function that returns => T

  27. 27

    Why does my function only returns the first object from the array

  28. 28

    Why can't the typeclass constraint be inferred and get ambiguous instead?

  29. 29

    jQuery function won't run when removed from context

HotTag

Archive