Jump to page: 1 2
Thread overview
Generic operator overloading for immutable types?
Jun 12, 2017
Gary Willoughby
Jun 12, 2017
H. S. Teoh
Jun 12, 2017
Gary Willoughby
Jun 12, 2017
arturg
Jun 12, 2017
Gary Willoughby
Jun 12, 2017
Ali Çehreli
Jun 12, 2017
H. S. Teoh
Jun 13, 2017
Gary Willoughby
Jun 13, 2017
Gary Willoughby
Jun 13, 2017
ag0aep6g
Jun 13, 2017
ag0aep6g
Jun 13, 2017
ag0aep6g
Jun 14, 2017
ag0aep6g
Jun 14, 2017
Kagamin
Jun 12, 2017
ketmar
June 12, 2017
On Mon, Jun 12, 2017 at 07:38:44PM +0000, Gary Willoughby via Digitalmars-d-learn wrote:
> In the following code is there any way to make the `opBinary` method generic to be able to accept immutable as well as a standard type? The code currently passes the unit test but I wonder if I could get rid of the duplication to overload the operator? I'm failing badly.

This is what inout was designed for:

 	public inout Rational opBinary(string op)(inout Rational rhs)
 	{
 		static if (op == "+")
 		{
 			return inout(Rational)(0, 0);
 		}
 		else
 		{
 			static assert(0, "Operator '" ~ op ~ "' not implemented");
 		}
 	}

That should do the trick.


T

-- 
Frank disagreement binds closer than feigned agreement.
June 12, 2017
In the following code is there any way to make the `opBinary` method generic to be able to accept immutable as well as a standard type? The code currently passes the unit test but I wonder if I could get rid of the duplication to overload the operator? I'm failing badly.


import std.stdio;

struct Rational
{
	public long numerator;
	public long denominator;

	public immutable Rational opBinary(string op)(immutable Rational rhs)
	{
		static if (op == "+")
		{
			return Rational(0, 0);
		}
		else
		{
			static assert(0, "Operator '" ~ op ~ "' not implemented");
		}
	}

	public Rational opBinary(string op)(Rational rhs)
	{
		static if (op == "+")
		{
			return Rational(0, 0);
		}
		else
		{
			static assert(0, "Operator '" ~ op ~ "' not implemented");
		}
	}
}

unittest
{
	auto foo = Rational(1, 3);
	auto bar = Rational(1, 6);
	writefln("%s", foo + bar);

	auto baz = immutable Rational(1, 3);
	auto qux = immutable Rational(1, 6);
	writefln("%s", baz + qux);
}

June 12, 2017
Gary Willoughby wrote:

> In the following code is there any way to make the `opBinary` method generic to be able to accept immutable as well as a standard type? The code currently passes the unit test but I wonder if I could get rid of the duplication to overload the operator? I'm failing badly.

	public inout(Rational) opBinary(string op)(inout(Rational) rhs) inout {
	  static if (op == "+") {
	    return Rational(0, 0);
	  } else {
	    static assert(0, "Operator '" ~ op ~ "' not implemented");
	  }
	}
June 12, 2017
I don't know how H. S. Teoh managed to answer 'before' I posted but thanks guys! :)
June 12, 2017
On Monday, 12 June 2017 at 19:51:37 UTC, Gary Willoughby wrote:
> I don't know how H. S. Teoh managed to answer 'before' I posted but thanks guys! :)

might be a bug, happened here http://forum.dlang.org/post/ohbr5l$2mng$1@digitalmars.com also.
June 12, 2017
On Monday, 12 June 2017 at 19:36:52 UTC, H. S. Teoh wrote:
> On Mon, Jun 12, 2017 at 07:38:44PM +0000, Gary Willoughby via Digitalmars-d-learn wrote:
>> In the following code is there any way to make the `opBinary` method generic to be able to accept immutable as well as a standard type? The code currently passes the unit test but I wonder if I could get rid of the duplication to overload the operator? I'm failing badly.
>
> This is what inout was designed for:
>
>  	public inout Rational opBinary(string op)(inout Rational rhs)
>  	{
>  		static if (op == "+")
>  		{
>  			return inout(Rational)(0, 0);
>  		}
>  		else
>  		{
>  			static assert(0, "Operator '" ~ op ~ "' not implemented");
>  		}
>  	}
>
> That should do the trick.
>
>
> T

Quick question about the signature, if I change it to (note the parens):

   public inout(Rational) opBinary(string op)(inout(Rational) rhs)

It no longer works, why is that?
June 12, 2017
On 06/12/2017 01:03 PM, Gary Willoughby wrote:
> On Monday, 12 June 2017 at 19:36:52 UTC, H. S. Teoh wrote:

>>      public inout Rational opBinary(string op)(inout Rational rhs)

> Quick question about the signature, if I change it to (note the parens):
>
>    public inout(Rational) opBinary(string op)(inout(Rational) rhs)
>
> It no longer works, why is that?

That's frequently faced issue with D. :) In the first declaration, inout applies to the member function. In the second one, it applies only to the return type. Walter has his rationale for doing it that way but it's confusing.

Ali

June 12, 2017
On Mon, Jun 12, 2017 at 01:08:13PM -0700, Ali Çehreli via Digitalmars-d-learn wrote:
> On 06/12/2017 01:03 PM, Gary Willoughby wrote:
> > On Monday, 12 June 2017 at 19:36:52 UTC, H. S. Teoh wrote:
> 
> >>      public inout Rational opBinary(string op)(inout Rational rhs)
> 
> > Quick question about the signature, if I change it to (note the parens):
> >
> >    public inout(Rational) opBinary(string op)(inout(Rational) rhs)
> >
> > It no longer works, why is that?
> 
> That's frequently faced issue with D. :) In the first declaration, inout applies to the member function.

More precisely, it applies to the `this` reference implicitly passed to the member function.  You need inout to apply to `this`, otherwise a.opBinary(b) won't work when a is an immutable instance.


> In the second one, it applies only to the return type. Walter has his rationale for doing it that way but it's confusing.
[...]

A few years ago we tried lobbying for the language to enforce putting modifiers that only apply to the function itself on the far right side, e.g.:

	public Rational opBinary(string op)(inout Rational rhs) inout

But it was rejected for various reasons.

Therefore, nowadays I always recommend writing parenthesis with type modifiers, so that the intent it unambiguous, i.e., always write `inout(Rational)` rather than `inout Rational`, unless you intend for `inout` to apply to the function rather than the return value. (And I apologize for the slip-up in my code example above, where I failed to do this for the function parameter.)


T

-- 
Маленькие детки - маленькие бедки.
June 13, 2017
On Monday, 12 June 2017 at 20:10:17 UTC, H. S. Teoh wrote:
> Therefore, nowadays I always recommend writing parenthesis with type modifiers, so that the intent it unambiguous, i.e., always write `inout(Rational)` rather than `inout Rational`, unless you intend for `inout` to apply to the function rather than the return value. (And I apologize for the slip-up in my code example above, where I failed to do this for the function parameter.)
>
>
> T

Ok, thanks.
June 13, 2017
On 6/12/17 3:51 PM, Gary Willoughby wrote:
> I don't know how H. S. Teoh managed to answer 'before' I posted but
> thanks guys! :)

D programmers are *that* good.

Seriously though, for NNTP connections, timestamp is taken from the submitter's PC.

-Steve
« First   ‹ Prev
1 2