Comparison operators and superprofits

How to create an object when its operators, such as:

operator > (Object obj1, Object obj2)  
operator < (Object obj1, Object obj2)

redefined PowerShell uses these operators?

In this way:

where-object { $CustomObject -gt 12 } 

will cause:

public static bool operator > (Object object1, Object object2)

Is it possible?

To clarify:

  • An object exists in a .NET assembly.
  • Object has overridden comparison operators
  • PowerShell does not seem to respect these statements.
+3
source share
4 answers

I do not believe that you can perform operator overloading in Powershell.

+1
source

PowerShell uses the IComparable interface to compare objects. At least this shows the following little experiment:

$src = @'
using System;
namespace Acme 
{
    public class Foo : IComparable
    {
        public Foo(int value)
        {
            this.Value = value;
        }

        public int Value { get; private set; }

        public static bool operator >(Foo foo1, Foo foo2)
        {
            Console.WriteLine("In operator >");
            return (foo1.Value > foo2.Value);
        }

        public static bool operator <(Foo foo1, Foo foo2)
        {
            Console.WriteLine("In operator <");
            return (foo1.Value < foo2.Value);
        }

        public int CompareTo(object obj)
        {
            Console.WriteLine("In CompareTo");
            if (obj == null) return 1;

            Foo foo2 = obj as Foo;
            if (foo2 == null) throw new ArgumentException("Not type Foo","obj");

            if (this.Value == foo2.Value)
            {
                return 0;
            }
            else if (this.Value > foo2.Value)
            {
                return 1;
            }
            else 
            {
                return -1;
            }
        }
    }
}
'@

Add-Type -TypeDefinition $src -Language CSharpVersion3

$foo1 = new-object Acme.Foo 4
$foo2 = new-object Acme.Foo 8

$foo1 -gt $foo2
In CompareTo
False
+12
source

#

PowerShell, #

        // overloading + operator
        public static Nimber operator +(Nimber left, Nimber right)
    {
        var length = (left.List.Count > right.List.Count) ? left.List.Count : right.List.Count;
        var list = new int[length];

        for (int i = 0; i < left.List.Count; i++)
        {
            list[i] = left.List[i];
        }

        for (int i = 0; i < right.List.Count; i++)
        {
            list[i] += right.List[i];
        }

        return new Nimber(list);
    }

posershell

Add-Type -Path $TheAssemblyPath
$n1 = New-Object nim.nimber (1,2,4) 
$n2 = New-Object nim.nimber (10,20,40,50) 
"n1=$n1 and n2=$n2"

$n3 = $n1 + $n2
"n3=$n3"

-

n1=1, 2, 4 and n2=10, 20, 40, 50
n3=11, 22, 44, 50
+2

You cannot perform operator overloading in PowerShell, but I believe that, as you stated, it will work, since PowerShell must obey the .NET operator overloading. When this does not work, what you often see are different objects on each side, that is, [int] -gt [string]. You can always try to explicitly distinguish both objects before comparison.

Hope this helps

0
source

Source: https://habr.com/ru/post/1727802/


All Articles