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

Object[] should cast to IEnumerable[<>] #16538

Open
5 tasks done
iRon7 opened this issue Dec 2, 2021 · 4 comments
Open
5 tasks done

Object[] should cast to IEnumerable[<>] #16538

iRon7 opened this issue Dec 2, 2021 · 4 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@iRon7
Copy link

iRon7 commented Dec 2, 2021

Prerequisites

Steps to reproduce

I am not quiet sure about this, but I would expect PowerShell to be able to cast (CanConvertTo) an object array (Object[]) to a specific IEnumerable. The issue occurred to me when I was trying to create an InvariantCultureIgnoreCase HashSet and immediately populate it in a single statement:

[System.Collections.Generic.HashSet[String]]::new(@('a', 'b', 'c', 'A', 'b'), [StringComparer]::InvariantCultureIgnoreCase)

Expected behavior

a
b
c

Actual behavior

MethodException: Cannot find an overload for "new" and the argument count: "2".
Exception             :
    Type        : System.Management.Automation.MethodException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot find an overload for "new" and the argument count: "2".
            HResult : -2146233087
        CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : MethodCountCouldNotFindBest
        InvocationInfo        :
            ScriptLineNumber : 1
            OffsetInLine     : 1
            HistoryId        : -1
            Line             : [System.Collections.Generic.HashSet[String]]::new(@('a', 'b', 'c', 'A', 'b'), [StringComparer]::InvariantCultureIgnoreCase)
            PositionMessage  : At line:1 char:1
                               + [System.Collections.Generic.HashSet[String]]::new(@('a', 'b', 'c', 'A …
                               + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  : System.Object CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Type, System.Object[], System.StringComparer)
    Message     : Cannot find an overload for "new" and the argument count: "2".
    Data        : System.Collections.ListDictionaryInternal
    Source      : Anonymously Hosted DynamicMethods Assembly
    HResult     : -2146233087
    StackTrace  :
   at CallSite.Target(Closure , CallSite , Type , Object[] , StringComparer )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
CategoryInfo          : NotSpecified: (:) [], MethodException
FullyQualifiedErrorId : MethodCountCouldNotFindBest
InvocationInfo        :
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : -1
    Line             : [System.Collections.Generic.HashSet[String]]::new(@('a', 'b', 'c', 'A', 'b'), [StringComparer]::InvariantCultureIgnoreCase)
    PositionMessage  : At line:1 char:1
                       + [System.Collections.Generic.HashSet[String]]::new(@('a', 'b', 'c', 'A …
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Workarround

To workaround this you will need specify the array item type ([string[]]):

[System.Collections.Generic.HashSet[String]]::new([String[]]@('a', 'b', 'c', 'A', 'b'), [StringComparer]::InvariantCultureIgnoreCase)

Related

This is probably related to the fact that I can't directly cast an object[] to a specific enumerable either:

[System.Collections.Generic.IEnumerable[String]]@('a', 'b', 'c')
InvalidArgument: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Collections.Generic.IEnumerable`1[System.String]".

But this (rather WET) statement works:

[System.Collections.Generic.IEnumerable[String]][String[]]@('a', 'b', 'c')
a
b
c

Related issues

@iRon7 iRon7 added the Needs-Triage The issue is new and needs to be triaged by a work group. label Dec 2, 2021
@SeeminglyScience
Copy link
Collaborator

Also related: #7651

@vexx32 vexx32 added Issue-Enhancement the issue is more of a feature request than a bug WG-Engine core PowerShell engine, interpreter, and runtime labels Dec 2, 2021
@SeeminglyScience SeeminglyScience added Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Apr 14, 2022
@SeeminglyScience SeeminglyScience self-assigned this Apr 14, 2022
@SeeminglyScience
Copy link
Collaborator

The Engine-WG discussed this last night and we agree that casting should be aware of a few key interfaces and how to translate them.

The big ones I can see being necessary for a first round is:

Interface Fallback
IList<T> T[]
ICollection<T> T[]
IEnumerable<T> T[]
IReadOnlyList<T> T[]
IReadOnlyCollection<T> T[]
Less important
IList object[]
ICollection object[]
IEnumerable object[]
IDictionary Hashtable
IDictionary<TKey, TValue> Dictionary<TKey, TValue>

Marking up for grabs.

@SeeminglyScience SeeminglyScience removed their assignment Apr 15, 2022
@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
@iRon7
Copy link
Author

iRon7 commented Nov 15, 2023

This is already labeled Up-for-Graps and therefore shouldn't be closed.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label May 13, 2024
@iRon7
Copy link
Author

iRon7 commented May 14, 2024

The issue still exists in PowerShell 7.4.2 and is already labeled Up-for-Graps and therefore shouldn't be closed. Or???

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Resolution-No Activity Issue has had no activity for 6 months or more label May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Up-for-Grabs Up-for-grabs issues are not high priorities, and may be opportunities for external contributors WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

3 participants