Re: How to define a c# class to support IronPython power operator **

Stéphane Lozier
 

There are a bunch of variations (e.g. using the name "__pow__", using the name "Power", static methods, instance methods), but here is one example:

        public class MyClass {
            private readonly double x;

            public MyClass(double x) {
                this.x = x;
            }

            public static MyClass __pow__(MyClass x, int y) {
                return new MyClass(Math.Pow(x.x, y));
            }

            public static MyClass __pow__(MyClass x, MyClass y) {
                return new MyClass(Math.Pow(x.x, y.x));
            }
        }

Stéphane


On Fri, Jul 5, 2019 at 4:43 PM Hailiang Shen <hailiang@...> wrote:

Hello all, I am defining a C# assembly to be used in IronPython. For one C# class, I need to support the IronPython power operator (**). For example in IronPython

 

MyClass1 ** 3 will automatically call my power function defined on my C# class.

MyClass1 ** MyClass2 will also automatically call my power function defined on my C# class.

 

It is simple to overload the overloadable operators https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/overloadable-operators, but I am not sure how to support the power operator. Could you suggest which C# operator I need to overload or is there one?

 

Hailiang

Join users@ironpython.groups.io to automatically receive all group messages.