-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
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)
PowerShell/src/System.Management.Automation/engine/runtime/CompiledScriptBlock.cs
Line 325 in bd6fdae
| 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:
PowerShell/src/System.Management.Automation/engine/lang/scriptblock.cs
Lines 572 to 577 in bd6fdae
| 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 } }