question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Empty immutable collections should be explicitly documented as singletons

See original GitHub issue

ImmutableList.of() says this:

Returns the empty immutable list. This set behaves and performs comparably to Collections.emptyList(), and is preferable mainly for consistency and maintainability of your code.

The use of the phrase “the empty immutable list” suggests that this method always returns the same object, as indeed it does. But this is implied only by the use of the definite article and the reference to Collections.emptyList(). It would be better to state it more explicitly. By comparison, Collections.emptyList() itself says this:

Implementation note: Implementations of this method need not create a separate List object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)

Of course the same applies to the other empty-collection methods.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ronshapirocommented, May 11, 2018

We treat ImmutableList as an interface in that sense. It has meaningful semantics that are frequently relevant for a caller to know. In the same way that you can return a Collection vs. a Set, even though Set doesn’t have any different methods, it is clearly a different type.

On Fri, May 11, 2018, 8:41 AM Ivo Šmíd notifications@github.com wrote:

I understand what you mean, but it works only when you have ImmutableList at signatures, but when you prefer coding against interface, you have List there…

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/guava/issues/2083#issuecomment-388352087, or mute the thread https://github.com/notifications/unsubscribe-auth/AAwY3dJfS_cvFa3ZOiXnEpUJssqgDsM0ks5txYbpgaJpZM4FFxpc .

0reactions
cpovirkcommented, Apr 19, 2021

(Oh, and I was also going to note that we could conceivably make similar changes for Optional.absent and quite possibly other types.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inaccurate documentation on immutable collections · Issue #2081 ...
Regarding the singleton return value, I would say it should either be documented clearly or not at all. All reactions.
Read more >
Are Python Empty Immutables Singletons? - Stack Overflow
No, Python's empty immutable containers are not guaranteed to be singletons*. The only guaranteed singletons are True , False , None , ...
Read more >
Collections::singletonList Vs. List::of - DZone Java
Although Collections::singletonList makes it explicitly clear that the returned list contains only one item, List.of(item) is also clear: " ...
Read more >
.NET Framework - Immutable Collections | Microsoft Learn
To create a builder instance, you need an immutable collection instance. You can start with an empty collection and use the ImmutableList.CreateBuilder<T> ...
Read more >
Collections (Java Platform SE 8 ) - Oracle Help Center
The empty set (immutable). Method Summary. All Methods Static Methods Concrete ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found