Re: Generic operator overloading for immutable types?

2017-06-14 Thread Kagamin via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 19:29:26 UTC, Gary Willoughby wrote: Is it possible for the `result` variable in the following code to be returned as an immutable type if it's created by adding two immutable types? Why do you even want that? Such plain data structure is implicitly convertible to

Re: Generic operator overloading for immutable types?

2017-06-14 Thread ag0aep6g via Digitalmars-d-learn
On 06/14/2017 03:47 AM, Steven Schveighoffer wrote: The fundamental difference is that const and immutable share a characteristic that mutable doesn't -- you can't mutate the data. (... through the reference at hand.) const and mutable share this: The data may be mutated from elsewhere.

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 7:51 PM, ag0aep6g wrote: On 06/14/2017 12:45 AM, Steven Schveighoffer wrote: No, the fact that immutable implicitly casts to const(inout) is a special property enabled by the knowledge that immutable data can NEVER change, so it's OK to assume it's (at least) const for all

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/14/2017 12:45 AM, Steven Schveighoffer wrote: No, the fact that immutable implicitly casts to const(inout) is a special property enabled by the knowledge that immutable data can NEVER change, so it's OK to assume it's (at least) const for all references. The same cannot be true of const

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 5:58 PM, ag0aep6g wrote: On 06/13/2017 10:50 PM, Steven Schveighoffer wrote: const(inout) actually *is* a thing :) It's a type constructor that can be implicitly cast from immutable. This has advantages in some cases. See (horribly written) table at the bottom if the inout function

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/13/2017 10:50 PM, Steven Schveighoffer wrote: const(inout) actually *is* a thing :) It's a type constructor that can be implicitly cast from immutable. This has advantages in some cases. See (horribly written) table at the bottom if the inout function section here:

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/13/17 3:58 PM, ag0aep6g wrote: On 06/13/2017 09:29 PM, Gary Willoughby wrote: Is it possible for the `result` variable in the following code to be returned as an immutable type if it's created by adding two immutable types? Qualify the return type as `inout`: inout(Rational)

Re: Generic operator overloading for immutable types?

2017-06-13 Thread ag0aep6g via Digitalmars-d-learn
On 06/13/2017 09:29 PM, Gary Willoughby wrote: Is it possible for the `result` variable in the following code to be returned as an immutable type if it's created by adding two immutable types? Qualify the return type as `inout`: inout(Rational) opBinary(/*...*/)(/*...*/) inout {/*...*/}

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn
On Tuesday, 13 June 2017 at 11:36:45 UTC, Steven Schveighoffer wrote: Nope, const works just fine. A clue is in your return type -- it's not inout! This should work: public Rational opBinary(string op)(Rational rhs) const If Rational had any indirections, then inout would be required, and

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/12/17 3:36 PM, H. S. Teoh via Digitalmars-d-learn wrote: On Mon, Jun 12, 2017 at 07:38:44PM +, 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

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Steven Schveighoffer via Digitalmars-d-learn
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

Re: Generic operator overloading for immutable types?

2017-06-13 Thread Gary Willoughby via Digitalmars-d-learn
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

Re: Generic operator overloading for immutable types?

2017-06-12 Thread H. S. Teoh via Digitalmars-d-learn
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

Re: Generic operator overloading for immutable types?

2017-06-12 Thread Ali Çehreli via Digitalmars-d-learn
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)

Re: Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn
On Monday, 12 June 2017 at 19:36:52 UTC, H. S. Teoh wrote: On Mon, Jun 12, 2017 at 07:38:44PM +, 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

Re: Generic operator overloading for immutable types?

2017-06-12 Thread arturg via Digitalmars-d-learn
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.

Re: Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn
I don't know how H. S. Teoh managed to answer 'before' I posted but thanks guys! :)

Re: Generic operator overloading for immutable types?

2017-06-12 Thread ketmar via Digitalmars-d-learn
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

Re: Generic operator overloading for immutable types?

2017-06-12 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Jun 12, 2017 at 07:38:44PM +, 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

Generic operator overloading for immutable types?

2017-06-12 Thread Gary Willoughby via Digitalmars-d-learn
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;