-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
[suggested trap] should not use ValueFromPipeline with more than one param #9
Comments
Thank you for the interesting case, I did not know about such a feature. This may be even useful in some scenarios. Nothing practical comes to my mind # Both commands output:
# $P1: v1
# $P2: v2
'v2' | foo v1
'v1' | foo -p2 v2 In other words, I can choose what to pipe, |
My mental model of parameter binding includes the concept of "conservation of parameters" (like conservation of mass/energy in the physical world). One input "thing" gets bound to one parameter. I cannot think of any other binding scenario (I mean I think the only other possibilities are "positional" and "named") where a single input object gets "duplicated" to more than one parameter. For example, consider positional binding. Suppose function foo
{
[CmdletBinding()]
param( [Parameter( Mandatory = $false, Position = 0 )]
[string] $P1,
[Parameter( Mandatory = $false, Position = 0 )] # NOTE: same position!
[string] $P2
)
process
{
try
{
Write-Host "`$P1: $P1" -Fore Magenta
Write-Host "`$P2: $P2" -Fore Magenta
}
finally { }
}
} If You could come up with an analogous situation for named binding, even: suppose you tried to write In short, while one could argue that this is simply the design of pipeline binding, I found it unexpected, and therefore considered it a candidate for a "trap", but I'm not sure what rubric you use to decide if something qualifies. |
I like the case and I do not mind to add it to the collection. I do not understand why |
Something like sane code that does not work as expected. |
I see what he's getting at. He expects that a particular pipeline input is
only bound to a single parameter.
I haven't ever had a reason to bind one to multiple parameters, but I could
envision having two different parameters wiht different types that bound to
the same parameter.
If you needed the pipeline value to be "convertible" to both types, that
would get it done. I don't know why you'd do it that way.
On the other hand, there's nothing "wrong" with doing something "unusual".
Unless you think this is a common error for someone to do, I don't see the
need to add it.
Just my opinion. :-)
…On Mon, Apr 22, 2019 at 8:59 PM Roman Kuzmin ***@***.***> wrote:
I'm not sure what rubric you use to decide if something qualifies [as a
trap].
Something like sane code that does not work as expected.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#9 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABKI2IW76Z7I3YSP2EU5IN3PRZUQBANCNFSM4HHQXFRA>
.
|
You're right; if it were to work as the other binding types, it should fail. |
I added the case to the collection, see ValueFromPipeline. |
I didn't notice an existing trap covering pipeline input getting bound to multiple parameters. Repro:
The text was updated successfully, but these errors were encountered: