Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToList produces unexpected result with arrays containing primitives #298

Open
GCHQDeveloper314 opened this issue Oct 24, 2022 · 0 comments
Labels
bug Confirmed or suspected bug question Specific query about part of the codebase
Milestone

Comments

@GCHQDeveloper314
Copy link
Member

The Javadoc for ToList states that it "takes an Object and converts it to a list. If the object is an array or iterable the items will be added to a new list. Otherwise a new list is created with the single value as an item". However, if an array (which is an object) containing primitives is used, the result is not as expected and is not the same as with arrays of objects.

Input
ToList().apply(new int[] {1, 2})
Expected result
List with values [1,2]
Actual result
List containing the inputted array [[1,2]]

This happens because the code for ToList only checks if the input is an array of Object, not simply if it's an array. This causes arrays of primitives to be treated the same as single objects, causing them to be wrapped inside a List. This same behaviour will be exhibited by ToSet and other similar Koryphe function classes.

An array is always an Object in Java, even if it contains primitives and not objects. To resolve this problem, either the ability to handle arrays of primitives should be added, or this type of input should be rejected and the description improved (see below).

It's worth noting that if the input is itself a primitive (e.g. ToList().apply(1)), then it will be autoboxed and converted to the equivalent object automatically. The description of "takes an Object" is not informative, as it is impossible not to pass an object to the method, because any primitives will be autoboxed to objects and an arrays of primitives is itself an object.

@GCHQDeveloper314 GCHQDeveloper314 added bug Confirmed or suspected bug question Specific query about part of the codebase labels Oct 24, 2022
@GCHQDeveloper314 GCHQDeveloper314 added this to the post-v2.0_backlog milestone Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed or suspected bug question Specific query about part of the codebase
Projects
None yet
Development

No branches or pull requests

1 participant