Skip to content
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

Join-String does not produce the same result as -join #8632

Closed
PetSerAl opened this issue Jan 12, 2019 · 12 comments
Closed

Join-String does not produce the same result as -join #8632

PetSerAl opened this issue Jan 12, 2019 · 12 comments
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module

Comments

@PetSerAl
Copy link
Contributor

Steps to reproduce

class C: System.Collections.IEnumerable {
    [string] ToString() { return 'Correct' }
    [System.Collections.IEnumerator] GetEnumerator() { return ('Not', 'Correct').GetEnumerator() }
}
[C]::new(), [C]::new(), [C]::new() -join ', '
[C]::new(), [C]::new(), [C]::new() | Join-String -Separator ', '

Expected behavior

Correct, Correct, Correct
Correct, Correct, Correct

Actual behavior

Correct, Correct, Correct
Not Correct, Not Correct, Not Correct

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.0-preview.3
PSEdition                      Core
GitCommitId                    6.2.0-preview.3-104-ge399b06cd23a8d9a7df9be5ed329471ba5b8eb82
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.2.0-preview.3}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
@vexx32
Copy link
Collaborator

vexx32 commented Jan 12, 2019

I don't think this is really avoidable. By the time the cmdlet receives input, the GetEnumerator() has already been called by the pipeline logic. 🙁

@PetSerAl
Copy link
Contributor Author

@vexx32

By the time the cmdlet receives input, the GetEnumerator() has already been called by the pipeline logic.

Any evidence to support this claim?

function My-Join-String { @($input) -join ', ' }
[C]::new(), [C]::new(), [C]::new() | My-Join-String

@vexx32
Copy link
Collaborator

vexx32 commented Jan 12, 2019

@PetSerAl Ah, I see! Okay, I misunderstood the original point.

Looks like it's enumerating each pipeline item, likely in an attempt to make pipeline input behave the same as Join-String -InputObject ([C]::new(), [C]::new(), [C]::new())

@mklement0 and I have discussed this a bit and have a code pattern that could help rectify it outlined in #4242 as you've seen.

@PetSerAl
Copy link
Contributor Author

@vexx32 If it enumerating each pipeline item, the it should produce Not, Correct, Not, Correct, Not, Correct instead of Not Correct, Not Correct, Not Correct.

@vexx32
Copy link
Collaborator

vexx32 commented Jan 12, 2019

Not quite. It calls the enumerator of your object to enumerate it, and receives back a two element array for each item. It doesn't then enumerate each of those arrays, that would be an extra level of enumeration. 🙂

I would guess that somewhere in its logic it converts these either implicitly or explicitly to string, which presumably forces PS to call ToString() on the resulting array.

@PetSerAl
Copy link
Contributor Author

That is the problem. Join-String should not do [C] -> [Array] -> [String] instead of [C] -> [String].

@iSazonov
Copy link
Collaborator

Are other cmdlets affected by this?

@iSazonov iSazonov added Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module labels Jan 12, 2019
@vexx32
Copy link
Collaborator

vexx32 commented Jan 12, 2019

I don't think a lot of cmdlets will be. Most cmdlets implement -InputObject strictly for pipeline use, so they can make some generally safe assumptions.

There may be some, but I don't know of any affected in the same way as this one is... Yet 😄

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

2 similar comments
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-No Activity Issue has had no activity for 6 months or more WG-Cmdlets-Utility cmdlets in the Microsoft.PowerShell.Utility module
Projects
None yet
Development

No branches or pull requests

3 participants