Re: TDPL: Operator Overloading

2010-08-26 Thread Philippe Sigaud
On Wed, Aug 25, 2010 at 17:27, Andrej Mitrovic andrej.mitrov...@test.comwrote: What would be really cool is if we had a property that returned a random value of any integrated type. And for user-defined types, maybe it would call a method with a special name. I guess one could make a template

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Tue, 24 Aug 2010 18:43:49 -0400, Andrej Mitrovic andrej.mitrov...@whatever.com wrote: I wasn't refering to the mixin, but the call to CheckedInt(). mixin compiles value ~ op ~ rhs.value, which in this case evaluates to 5 + 5 and the whole call becomes CheckedInt(10). What I don't

Re: TDPL: Operator Overloading

2010-08-25 Thread bearophile
Steven Schveighoffer: Inside a template instantiation, the template name without template parameters is equivalent to the current instantiation. It saves a lot of typing. And causes some troubles :-) It's a barter, and I am not sure it's a good one. Bye, bearophile

Re: TDPL: Operator Overloading

2010-08-25 Thread Andrej Mitrovic
Ok I think I am kind of getting this. The template name inside a template is it's instantiation. I can do CheckedInt.variable and get back the value of variable in the current instantiation. The trouble is, when you do a call like CheckedInt() you will loose all other data that you had before:

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Wed, 25 Aug 2010 09:55:47 -0400, bearophile bearophileh...@lycos.com wrote: Steven Schveighoffer: Inside a template instantiation, the template name without template parameters is equivalent to the current instantiation. It saves a lot of typing. And causes some troubles :-) It's a

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Wed, 25 Aug 2010 10:01:31 -0400, Andrej Mitrovic andrej.mitrov...@gmail.com wrote: Ok I think I am kind of getting this. The template name inside a template is it's instantiation. I can do CheckedInt.variable and get back the value of variable in the current instantiation. The trouble

Re: TDPL: Operator Overloading

2010-08-25 Thread Andrej Mitrovic
That wasn't my expectation, but nevermind. The example is ok in this case, it's just that care needs to be taken when making operator overloads. So maybe I overreacted a little. :) Steven Schveighoffer Wrote: A struct is a value type, so you are making a copy regardless. Your expectation

Re: TDPL: Operator Overloading

2010-08-25 Thread Andrej Mitrovic
Interesting. :) The following seems to work, although I don't know if that's a good idea?: struct CheckedInt(N) // if(isIntegral!N) { void foo(N n) { } unittest { CheckedInt ci; ci.foo(N.init); } } unittest { CheckedInt!int ci1; CheckedInt!uint

TDPL: Operator Overloading

2010-08-24 Thread Andrej Mitrovic
This is a shortened version of some operator overloading code from page 372 (although some code is from pages before it), sorry for the long post: module binary_ops; import std.stdio : writeln; import std.traits; import std.exception; unittest { auto foo = CheckedInt!(int)(5); auto bar

Re: TDPL: Operator Overloading

2010-08-24 Thread Yao G.
On Tue, 24 Aug 2010 17:19:25 -0500, Andrej Mitrovic andrej.mitrov...@whatever.com wrote: [snip] struct CheckedInt(N) if (isIntegral!N) { private N value; this(N value) { this.value = value; } @property auto Value() { return value; } //

Re: TDPL: Operator Overloading

2010-08-24 Thread Andrej Mitrovic
Yao G. Wrote: That's a bug. The return value should be CheckedInt(result); I'll add that to the errata. Yao G. Wrote: http://www.digitalmars.com/d/2.0/mixin.html I wasn't refering to the mixin, but the call to CheckedInt(). mixin compiles value ~ op ~ rhs.value, which in this case

Re: TDPL: Operator Overloading

2010-08-24 Thread Yao G.
On Tue, 24 Aug 2010 17:43:49 -0500, Andrej Mitrovic andrej.mitrov...@whatever.com wrote: I wasn't refering to the mixin, but the call to CheckedInt(). mixin compiles value ~ op ~ rhs.value, which in this case evaluates to 5 + 5 and the whole call becomes CheckedInt(10). Sorry. My

Re: TDPL: Operator Overloading

2010-08-24 Thread Ali Çehreli
Andrej Mitrovic wrote: Yao G. Wrote: That's a bug. The return value should be CheckedInt(result); I'll add that to the errata. Yao G. Wrote: http://www.digitalmars.com/d/2.0/mixin.html I wasn't refering to the mixin, but the call to CheckedInt(). mixin compiles value ~ op ~ rhs.value,

Re: TDPL: Operator Overloading

2010-08-24 Thread bearophile
Ali Çehreli: It is the same in C++: the name of the template is equivalent to the current instantiation of the template. ... It seems to be the same in D. I don't know whether this is intended, or just a left over from the C++ parts of dmd. (I assume dmd shares code with the Digital Mars