#AutoClosable and lists/hashmaps
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
wdym, List and Map don't implement AutoCloseable
no, when the .remove() function is called does it call AutoClosable.close() before removing it from the list
for the object that is being removed
on what ? the object inside the list ? why would it ?
yes, the object that is being removed inside the list. it would do this since it's being removed and will not be used anymore
how can the list know that ? only the code which added to the list can know such a thing
the list would know that since... it's being removed? I don't understand
if you remove an object from a list you most likely don't want it
how can the list know remove = the item will never be used again ?
also note two things
- The code creating something that can be closed should be the one closing it, not the method the object is passed into, that's because of responsibilities : a piece of code should be responsible for what it did, and what it did only, not more, not less
- Methods shouldn't have side effects or anything that is not included in the name of the method, and so, a remove() method should only remove, it should never modify the removed object
@ashen mesa
thanks for this info
And by 1), I mean :
void a() {
try(Scanner scanner = ...) { // Creates scanner here
b(scanner);
} // Auto close scanner here
}
void b(Scanner scanner) {
// b hasn't created scanner, and so it should never close it !
println(scanner.nextLine());
}
@ashen mesa
I understand, thanks