Skip to content

PowerShell classes still can't create value types #10669

Closed

Description

@Jaykul

Steps to reproduce

Try to implement any interfaces that is intended to be implemented in terms of yourself, E.g. IEquatable, IComparable:

class ServerName : Object, System.IEquatable[ServerName] {
    [string]$ComputerName
    [bool] Equals([ServerName]$other) {
        return $this.ComputerName.Equals($other.ComputerName);
    }
}

Expected behavior

I should be able to write IEquatable<T> and IComparable<T> classes 

Actual behavior

A runtime compile error:

An error occurred while creating the pipeline.
+ CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException                                                                 + FullyQualifiedErrorId : RuntimeException                                  

Environment data

Any version of PowerShell since classes were introduced, up to and including 7 Pre 4

Activity

  1. added
    Issue-Question
    on Oct 1, 2019
  2. changed the title [-]PowerShell classes still can't implement interfaces on themselves[/-] [+]PowerShell classes still can't create value types[/+] on Oct 1, 2019
  3. mklement0 commented on Oct 1, 2019

    @mklement0
    Contributor

    It's obviously not the same, but just to mention an interim workaround: use [object] instead of your own type.

  4. iSazonov commented on Oct 2, 2019

    @iSazonov
    Collaborator
  5. added
    WG-Engine
    on Oct 2, 2019
  6. SeeminglyScience commented on Oct 2, 2019

    @SeeminglyScience
    Contributor

    Yeah this does repro without the specifics of #10623 (e.g. without the making the engine check overloads)

    PS> Add-Type -IgnoreWarnings -TypeDefinition 'public interface ISomething<T> { }'
    PS> class Test : ISomething[Test] { }
    # At line:1 char:14
    # + class Test : ISomething[Test] { }
    # +              ~~~~~~~~~~~~~~~~
    # Unable to find type [ISomething[Test]].
    #     + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    #     + FullyQualifiedErrorId : TypeNotFound

    Presumably because it's attempting type resolution without taking the current type definition into account.

    Also about valuetypes (assuming the title wasn't a mistake) #10231 is also relevant.

  7. essentialexch commented on Oct 2, 2019

    @essentialexch

    This works. But shouldn't. And just like Michael Klement (@mklement0) wrote, it's not the same as what you want, but a workaround. (I got seriously distracted going down the rabbit hole that is stuff not implemented by classes.)

    Class ServerName: System.Collections.IEqualityComparer 
    {
        [String] $ComputerName
    
        ServerName( [String] $serverName )
            {
                $this.ComputerName = $serverName
            }
        ServerName()
            {
                $this.ComputerName = ''
            }
    
        [Bool] Equals( $x, $y)  { return $x -eq $y        }
        [Bool] Equals( $x )     { return $this.ComputerName -eq $x }
        [Int] GetHashCode( $x ) { return $x.GetHashCode() }
    }
    
    $b = [ServerName]::new( 's1' )
    $b -eq 's1'
    $b -eq 's2'
    $b.GetHashCode( $b )
    
    
  8. JustinGrote commented on Jan 13, 2020

    @JustinGrote
    Contributor

    EDIT: Was case sensitive per Patrick Meinecke (@SeeminglyScience), ignore for purposes of this issue

    I came across this issue trying to implement IObserver for CIM to do get-ciminstance asynchronously basically.

    using namespace Microsoft.Management.Infrastructure
    class CimObserver : Iobserver[CimInstance] {
        [void] onNext(
            [CimInstance]$cimInstance
        ) {$null}
    }
    At line:2 char:1
    + class CimObserver : Iobserver[CimInstance] {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Error during creation of type "CimObserver". Error message:
    Method 'OnNext' in type 'CimObserver' from assembly 'PowerShell Class Assembly, Version=1.0.0.8, Culture=neutral, PublicKeyToken=null' does not have an implementation.
    

    Unless someone can tell me what I'm doing wrong.

  9. SeeminglyScience commented on Jan 13, 2020

    @SeeminglyScience
    Contributor

    Justin Grote (@JustinGrote) Method names are case sensitive for declaration:

    using namespace Microsoft.Management.Infrastructure
    
    class CimObserver : IObserver[CimInstance] {
        [void] OnNext([CimInstance] $value) {}
        [void] OnError([Exception] $exception) {}
        [void] OnCompleted() {}
    }

    Though be aware that it's likely that OnNext will be called on a thread without a default runspace. If that's the case, the method will just throw.

  10. JustinGrote commented on Jan 13, 2020

    @JustinGrote
    Contributor

    Patrick Meinecke (@SeeminglyScience) thanks. Believe it or not it totally worked :) Full code:

    using namespace Microsoft.Management.Infrastructure
    class CimObserver : System.IObserver[CimInstance] {
        [void] OnNext(
            [CimInstance]$ciminstance
        ) {
            write-host -fore green $cimInstance
        }
        [void] OnError([Exception]$Exception) {throw $exception}
        [void] OnCompleted() {$this.Unsubscribe()} 
    }
    $query = (new-cimsession).QueryInstancesAsync('root/cimv2','WQL','SELECT * FROM Win32_ComputerSystem')
    $observer = new-object CimObserver
    $query.subscribe($observer)

    Outputted the textual representation in green to the host.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  14. added
    Resolution-No Activity
    on Nov 16, 2023
  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-No ActivityWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions