Skip to content

Allow anonymous functions to be treated as filters #9731

@vexx32

Description

@vexx32

Summary of the new feature/enhancement

If we permit the IsFilter property on a scriptblock to be either internally or publicly settable, we can make more effective use of anonymous scriptblocks as impromptu filters. These could be utilised in a pipeline context to emulate ForEach-Object without a lot of the extra features and overhead afforded that cmdlet, for scenarios where speed is significantly more important.

Proposed technical implementation details (optional)

internal bool IsFilter { get; private set; }

This property needs to have at least internal set so that the following property that references it can also be set here instead of just throwing:

public bool IsFilter
{
get { return _scriptBlockData.IsFilter; }
set { throw new PSInvalidOperationException(); }
}

With that permitted, we can perhaps implement an operator or an attribute such that this becomes possible as an impromptu ForEach-Object with none of the fluff.

1..10 | & [filter]{ $_ % 3 } 

Currently, this is possible by specifying the process block, but this syntax becomes quite cluttered very easily:

1..10 | & { process { $_ % 3 } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions