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.

Array Extension Methods

See original GitHub issue

Is there a way to create an extension method for an array type? e.g.

@Extension
public class ArrayExt{

    public static void sort(@This int[] array){
        Arrays.sort(array);
    }

    //or, for objects...
    public static <T> void sort(@This T[] array){
        Arrays.sort(array);
    }
}

The documentation reads,

the remainder of the package must be the qualified name of the extended class

…but arrays are not real classes, and thus don’t have a fully qualified name.

Extensions made for java.lang.Object do function properly and show up in autocompletion for array types, but placing methods like sort() in Object would pollute autocompletion for non-array types and force me to use casting.


Related ““issue””: In my tests, I attempted creating an @Extension class in an empty package, which caused a crash due to ExtensionTransformer#getExtendedClassName calling toString() on getPackageName(), which was null.

This shouldn’t have compiled anyway, but a nicer error message than a NullPointerException could be used (although I don’t see this being much of a problem).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rsmckinneycommented, Jul 25, 2020

in progress

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to extend arrays in C#? - Stack Overflow
I am planning to add a method to arrays that converts it to a IEnumerable even if it is multidimensional. Not related to...
Read more >
How to add "Add" extension method to array? - MSDN
The correct extensions to apply would be this: int[] array = new int[] {1,2,3}; array = array. Concat(new int[] { 4 }). ToArray();...
Read more >
All C# extension methods for type array - ExtensionMethod.NET
All C# extension methods for type array · ToList<T>(Func<object, T> func) · ConvertTo<T> · SetAllValues · Shuffle · ToImage · Combine · REExtract....
Read more >
array.extend() | Dev Center - Electric Imp
This method appends all the elements in the array passed as its parameter to the end of the target array. So if the...
Read more >
Extension methods in C# .NET part 2: arrays, collections ...
Introduction We'll continue our discussion of extension methods by looking at arrays, collections and other types.
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