Skip to content

Feature Request: Support +/-/+=/-= operators for all IList types #13152

Description

@JustinGrote

Summary of the new feature/enhancement

Using modern types such as List/OrderedList/SortedList/etc. is becoming more common for performance reasons, but the array manipulation operators reconstruct these as [Object[]] which is not ideal for many already discussed reasons, and using other syntax such as .Add() is awkward especially for newer Powershell Users. Dictionary does not have this issue as the property assignment operators work fine.

I propose that, if a class implements IList and does not have op_Addition or op_Subtraction already defined, that the operators will use the standard IList Add and Remove methods to manipulate the array and preserve it rather than recasting all the contents into a new [Object[]].

This will allow users who want to use more advanced types for performance to be able to do so without breaking any behavior of @() syntax or normal powershell user experience.

NOTE: #5643 proposed a lot of syntactic sugar around lists or replacing the default type with lists. This Feature Request is smaller in scope but compatible and is less likely to cause breaking regressions.

Potential Breaking Changes

  1. Scripts that anticipate the existing behavior of casting to an [Object[]] and work around it could break, but it would have to be very specific and very rare edge case, for instance something that recasts it to a list would simply just cast a list to a list and not break. Could be controlled with a preference variable

EDITS

  1. Narrowed scope to just += and -= operators due to @SeeminglyScience's valid concerns

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions