Code Monkey home page Code Monkey logo

Comments (3)

bfiete avatar bfiete commented on July 23, 2024 1

But, yes- @Ruzihm did find a different issue- there are not good error messages for attempting to invoke an operator overload dynamically from an interface. These interfaces were meant only to be used as generic constraints, not for dynamic dispatch.

The issue is that the return type from these operator overloads is 'Self'. This is not allowed since 'Self' infers the type implementing this interface (the concrete type). If you are dynamically calling a Self-returning interface method on a boxed valuetype, then that return value would have to be boxed by the callee --- which I thought I couldn't allow since it infers heap allocation that no one will know to clean up, but now I realize it COULD be implemented with a side stack.

So, better error message for now, but dynamic dispatch for operator overloads is going in the "future features" pile - but with a fairly low priority.

from beef.

Ruzihm avatar Ruzihm commented on July 23, 2024

Shouldn't it rather be a different interface such as IOpSubtractable ? IOpNegatable specifically defines the unary negate operator, e.g.:

IOpNegatable x = (Float)5.0;
IOpNegatable y = -x;

Console.Write("{}", (Float)y);

Which... actually doesn't even compile because "Operator can only be used on numeric types". What's with that?


But this works as expected:

using System;

namespace Program
{
	class Foobar : IOpNegatable
	{
		public int value;

		public this(int val)
		{
			value = val;
		}

		public static Self operator -(Self me)
		{
			let result = new Foobar();
			result.value = -me.value;
			return result;
		}

		override public void ToString(String strBuffer)
		{
			strBuffer..AppendF("{}",value);
		}
	}

    class Program
    {
        public static void Main()
        {
           	Foobar x = scope:: Foobar(5);

		Foobar y = -x;
                defer delete y;

		Console.Write("{}", y);

		Console.In.Read();

        }
    }
}

from beef.

damianday avatar damianday commented on July 23, 2024

Agreed after giving a bit more thought a new interface IOpSubtractable is the correct approach

from beef.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.