Is List f() a useful signature? Is there any problem with it / using it?
Is <T> List<? extends T> f()
a useful signature? Is there any problem with it / using it?
This was an interview question. I know this:
- It compiles fine
- Use it like
List<? extends Number> lst = obj.<Number>f()
, and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)
Does that fully answer the question?
java
add a comment |
Is <T> List<? extends T> f()
a useful signature? Is there any problem with it / using it?
This was an interview question. I know this:
- It compiles fine
- Use it like
List<? extends Number> lst = obj.<Number>f()
, and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)
Does that fully answer the question?
java
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
You can still calladd(null)
etc.
– Andy Turner
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Using type inference you can just doList<? extends Number> lst = s.f();
. Also, you can doNumber number = lst.get(0); System.out.println(number);
.
– DodgyCodeException
49 mins ago
add a comment |
Is <T> List<? extends T> f()
a useful signature? Is there any problem with it / using it?
This was an interview question. I know this:
- It compiles fine
- Use it like
List<? extends Number> lst = obj.<Number>f()
, and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)
Does that fully answer the question?
java
Is <T> List<? extends T> f()
a useful signature? Is there any problem with it / using it?
This was an interview question. I know this:
- It compiles fine
- Use it like
List<? extends Number> lst = obj.<Number>f()
, and then I can call on lst only those List methods that do not contain T in their signatures (say, isEmpty(), size(), but not add(T), remove(T)
Does that fully answer the question?
java
java
asked 59 mins ago
user10777718
564
564
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
You can still calladd(null)
etc.
– Andy Turner
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Using type inference you can just doList<? extends Number> lst = s.f();
. Also, you can doNumber number = lst.get(0); System.out.println(number);
.
– DodgyCodeException
49 mins ago
add a comment |
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
You can still calladd(null)
etc.
– Andy Turner
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Using type inference you can just doList<? extends Number> lst = s.f();
. Also, you can doNumber number = lst.get(0); System.out.println(number);
.
– DodgyCodeException
49 mins ago
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
You can still call
add(null)
etc.– Andy Turner
51 mins ago
You can still call
add(null)
etc.– Andy Turner
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Using type inference you can just do
List<? extends Number> lst = s.f();
. Also, you can do Number number = lst.get(0); System.out.println(number);
.– DodgyCodeException
49 mins ago
Using type inference you can just do
List<? extends Number> lst = s.f();
. Also, you can do Number number = lst.get(0); System.out.println(number);
.– DodgyCodeException
49 mins ago
add a comment |
3 Answers
3
active
oldest
votes
I interpret "is it a useful signature" to mean "can you think of a use-case for it".
T
is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.
Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.
Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends
in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.
Well, looking very desperately for a contrived way of making it useful, you could have called asetReturnType(Class<?> cls)
method on the same object beforehand, and thenf()
would use that information to return a list of the required type.
– DodgyCodeException
44 mins ago
add a comment |
I'd consider it not useful because List<? extends T>
gives you the same amount of information List<T>
does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T>
you wont be able to use your List<? extends T>
even so what ever the function does would work just fine as there are T
s in your list.
Just using List<T>
is better.
EDIT
This answer is more general about using List
and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.
New contributor
add a comment |
The official Generics tutorial suggests not to use wildcard return types.
"These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."
The reasoning given isn't exactly convincing, though.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004821%2fis-t-list-extends-t-f-a-useful-signature-is-there-any-problem-with-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I interpret "is it a useful signature" to mean "can you think of a use-case for it".
T
is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.
Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.
Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends
in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.
Well, looking very desperately for a contrived way of making it useful, you could have called asetReturnType(Class<?> cls)
method on the same object beforehand, and thenf()
would use that information to return a list of the required type.
– DodgyCodeException
44 mins ago
add a comment |
I interpret "is it a useful signature" to mean "can you think of a use-case for it".
T
is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.
Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.
Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends
in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.
Well, looking very desperately for a contrived way of making it useful, you could have called asetReturnType(Class<?> cls)
method on the same object beforehand, and thenf()
would use that information to return a list of the required type.
– DodgyCodeException
44 mins ago
add a comment |
I interpret "is it a useful signature" to mean "can you think of a use-case for it".
T
is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.
Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.
Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends
in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.
I interpret "is it a useful signature" to mean "can you think of a use-case for it".
T
is determined at the call site, not inside the method, so there are only two things that you can return from the method: null or an empty list.
Given that you can create both of these values in roughly as much code as invoking this method, there isn't really a good reason to use it.
Actually, another value that can be safely returned is a list where all of the elements are null. But this isn't useful either, since you can only invoke methods which add or remove literal null from the return value, because of the ? extends
in the type bound. So all you've got is thing which counts the number of nulls it contains. Which isn't useful either.
edited 43 mins ago
answered 47 mins ago
Andy Turner
80.3k879133
80.3k879133
Well, looking very desperately for a contrived way of making it useful, you could have called asetReturnType(Class<?> cls)
method on the same object beforehand, and thenf()
would use that information to return a list of the required type.
– DodgyCodeException
44 mins ago
add a comment |
Well, looking very desperately for a contrived way of making it useful, you could have called asetReturnType(Class<?> cls)
method on the same object beforehand, and thenf()
would use that information to return a list of the required type.
– DodgyCodeException
44 mins ago
Well, looking very desperately for a contrived way of making it useful, you could have called a
setReturnType(Class<?> cls)
method on the same object beforehand, and then f()
would use that information to return a list of the required type.– DodgyCodeException
44 mins ago
Well, looking very desperately for a contrived way of making it useful, you could have called a
setReturnType(Class<?> cls)
method on the same object beforehand, and then f()
would use that information to return a list of the required type.– DodgyCodeException
44 mins ago
add a comment |
I'd consider it not useful because List<? extends T>
gives you the same amount of information List<T>
does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T>
you wont be able to use your List<? extends T>
even so what ever the function does would work just fine as there are T
s in your list.
Just using List<T>
is better.
EDIT
This answer is more general about using List
and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.
New contributor
add a comment |
I'd consider it not useful because List<? extends T>
gives you the same amount of information List<T>
does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T>
you wont be able to use your List<? extends T>
even so what ever the function does would work just fine as there are T
s in your list.
Just using List<T>
is better.
EDIT
This answer is more general about using List
and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.
New contributor
add a comment |
I'd consider it not useful because List<? extends T>
gives you the same amount of information List<T>
does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T>
you wont be able to use your List<? extends T>
even so what ever the function does would work just fine as there are T
s in your list.
Just using List<T>
is better.
EDIT
This answer is more general about using List
and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.
New contributor
I'd consider it not useful because List<? extends T>
gives you the same amount of information List<T>
does and where ever you want to use the list you will be stuck with this wildcard. Which means if you have a function taking a List<T>
you wont be able to use your List<? extends T>
even so what ever the function does would work just fine as there are T
s in your list.
Just using List<T>
is better.
EDIT
This answer is more general about using List
and wildcards.
@Andy Turner gave a more specific (and probably better) answer to the question.
New contributor
edited 44 mins ago
New contributor
answered 50 mins ago
Kanjiu
2369
2369
New contributor
New contributor
add a comment |
add a comment |
The official Generics tutorial suggests not to use wildcard return types.
"These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."
The reasoning given isn't exactly convincing, though.
add a comment |
The official Generics tutorial suggests not to use wildcard return types.
"These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."
The reasoning given isn't exactly convincing, though.
add a comment |
The official Generics tutorial suggests not to use wildcard return types.
"These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."
The reasoning given isn't exactly convincing, though.
The official Generics tutorial suggests not to use wildcard return types.
"These guidelines do not apply to a method's return type. Using a wildcard as a return type should be avoided because it forces programmers using the code to deal with wildcards."
The reasoning given isn't exactly convincing, though.
answered 15 mins ago
togorks
285
285
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54004821%2fis-t-list-extends-t-f-a-useful-signature-is-there-any-problem-with-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Returning a wildcard is not really useful
– NEGR KITAEC
56 mins ago
You can still call
add(null)
etc.– Andy Turner
51 mins ago
Related stackoverflow.com/questions/918026/list-extends-mytype
– user7294900
51 mins ago
Using type inference you can just do
List<? extends Number> lst = s.f();
. Also, you can doNumber number = lst.get(0); System.out.println(number);
.– DodgyCodeException
49 mins ago