Skip to content

PowerShell IComparable with subclasses #6831

Closed
@mlocati

Description

@mlocati

Steps to reproduce

Class BaseClass : System.IComparable
{
    [int] $Value
    BaseClass([int] $v)
    {
        $this.Value = $v
    }
    [int] CompareTo($that)
    {
        If (-Not($that -is [BaseClass])) {
            Throw "Not comparable!!"
        }
        return $this.Value - $that.Value
    }
}
Class SubClassA : BaseClass
{
    SubClassA([int] $v) : base($v)
    {
    }
}
Class SubClassB : BaseClass
{
    SubClassB([int] $v) : base($v)
    {
    }
}
$base1 = [BaseClass]::new(1)
$base2 = [BaseClass]::new(2)
$subA1 = [SubClassA]::new(1)
$subB2 = [SubClassB]::new(2)
Write-Output ('$base1 -lt $base2 -> ' + ($base1 -lt $base2))
Write-Output ('$subA1 -lt $subB2 -> ' + (([BaseClass]$subA1) -lt ([BaseClass]$subB2)))

Expected behavior

$base1 -lt $base2 -> True
$subA1 -lt $subB2 -> True

Actual behavior

$base1 -lt $base2 -> True
Could not compare "SubClassA" to "SubClassB". Error: "Cannot convert the "SubClassB" value of type "SubClassB" to type "SubClassA"."

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.1.0-preview.2
PSEdition                      Core
GitCommitId                    v6.1.0-preview.2
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions