Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Tue, 24 Aug 2010 18:43:49 -0400, Andrej Mitrovic 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 understand is how you can con

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 befo

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Wed, 25 Aug 2010 09:55:47 -0400, bearophile 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 barter, and I am not sure it

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Wed, 25 Aug 2010 10:01:31 -0400, Andrej Mitrovic 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 is, when you do a call

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 > expectatio

Re: TDPL: Operator Overloading

2010-08-25 Thread Steven Schveighoffer
On Tue, 24 Aug 2010 18:19:25 -0400, Andrej Mitrovic wrote: What I don't understand is how the constructor can be called like that. In my example the mixin would convert the code to: return CheckedInt(10); But if I ever tried a call like "CheckedInt(10)" in a unittest block, it wouldn't

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 c

Re: TDPL: Operator Overloading

2010-08-25 Thread Andrej Mitrovic
Although that might not work with floats since that would call foo with a NaN value, and if it used that, well that wouldn't really work. I could use N.max, but that doesn't work with strings. Andrej Mitrovic Wrote: > The following seems to work, although I don't know if that's a good idea?:

Re: TDPL: Operator Overloading

2010-08-25 Thread Andrej Mitrovic
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 function that would do just that. Andrej Mitrovic Wrote: > Although that might

More TDPL overloads

2010-08-25 Thread Andrej Mitrovic
Page 373 adds another operator overload for use with integral numbers (CheckedInt op Int). But it conflicts with the previos template (CheckedInt op CheckedInt): module binary_ops; import std.stdio : writeln; import std.traits; import std.exception; unittest { auto foo = CheckedInt!(int)(5

Re: More TDPL overloads

2010-08-25 Thread Steven Schveighoffer
On Wed, 25 Aug 2010 16:54:19 -0400, Andrej Mitrovic wrote: Page 373 adds another operator overload for use with integral numbers (CheckedInt op Int). But it conflicts with the previos template (CheckedInt op CheckedInt): module binary_ops; import std.stdio : writeln; import std.traits;

Re: Ways to initialize static arrays

2010-08-25 Thread Philippe Sigaud
2010/8/23 Stanislav Blinov > I have a struct template (let's call it S) that wraps an array of N > elements. N is set at compile time via template parameter. S uses static > array as storage (T[N]). I need a set of constants of type S which I'd like > to be evaluatable at compile time. I can crea

easier way?

2010-08-25 Thread Jason Spencer
I have the following sample program: --- import std.algorithm, std.stdio; real mean(T: Elem[], Elem)(T data) { return reduce!("a + b")(0.0, data) / data.length; } real mean(T: Elem[][], Elem)(T data) { real partSum = 0; int count = 0; foreach (row; data) { partSum += reduce!

Three floating point questions

2010-08-25 Thread bearophile
This program prints (dmd 2.048): 7ff4 7ffc // program #1 import std.stdio: writeln, writefln; import std.string: format; void main() { string toHex(T)(T x) { string result; ubyte* ptr = cast(ubyte*)&x; foreach (i; 0 .. T.sizeof) resul

Re: easier way?

2010-08-25 Thread bearophile
Jason Spencer: > Is there a better way to lay this out? Is this good enough for you? import std.stdio: writefln; import std.algorithm: reduce; import std.traits: isArray; real mean(T)(T[] array) if (!isArray!T) { return reduce!q{a + b}(0.0, array) / array.length; } real mean(T)(T[][] mat)

Re: easier way?

2010-08-25 Thread bearophile
> In D there are no built-in rectangular arrays. But probably a nD array struct will be added to Phobos in the following months. You may even write it yourself, for Phobos. As a less nice alternative, you may use a dynamic array of dynamic arrays and use the function preconditions (design by co

Re: Three floating point questions

2010-08-25 Thread Don
bearophile wrote: This program prints (dmd 2.048): 7ff4 7ffc // program #1 import std.stdio: writeln, writefln; import std.string: format; void main() { string toHex(T)(T x) { string result; ubyte* ptr = cast(ubyte*)&x; foreach (i; 0 .. T.sizeo

Re: Three floating point questions

2010-08-25 Thread bearophile
Don: > > Do you know what cast(ulong) is doing here? > > Turning it from a signalling nan to a quiet nan. I really really didn't know this. Is this written somewhere in the D docs? :-) > You mean, because it's a negative nan? Yes, I mean that. Is a negative nan a meaningful concept? Is it a tr

Re: easier way?

2010-08-25 Thread Jason Spencer
== Quote from bearophile (bearophileh...@lycos.com)'s article > Jason Spencer: > > Is there a better way to lay this out? > Is this good enough for you? It's certainly better. I'll have to look at how this plays with some of my other usage, where dimensions come into play, but this is definitely

Re: easier way?

2010-08-25 Thread bearophile
Jason Spencer: > Stupid stuff, that won't account for varying row sizes (which isn't > anything I need anyway.) Things like (&array[$][$] - > &array[0][0])/sizeof(array[0][0]), properly cast, etc. Be careful, you risk doing a mess :-) Dynamic arrays of the rows are not guaranteed to be contiguou

Re: easier way?

2010-08-25 Thread Jason Spencer
== Quote from bearophile (bearophileh...@lycos.com)'s article > I suggest you to create a templated struct where the template arguments are the element type and the number of dimensions. Do you mean just the number of dimensions, or do you mean the size along each dimension? Knowing just the # of

Re: easier way?

2010-08-25 Thread bearophile
Jason Spencer: > Do you mean just the number of dimensions, or do you mean the size > along each dimension? I mean just the number of dimensions. > Knowing just the # of dimensions won't tell me > the total size or how to index. I need the size of each dimension. If you need that information

Re: easier way?

2010-08-25 Thread bearophile
Jason Spencer: > Knowing just the # of dimensions won't tell me > the total size or how to index. I need the size of each dimension. If you create such structs, you do what you want, so it represents a nD rectangular array. The total size is computed by the product of n runtime values stored in

Compound assignment operators

2010-08-25 Thread Yao G.
Is there a way to make compound assignment operators (-=, +=, *= and friends) work with D's operator overload regime? I can't make them work. struct Foo { this( int foo ) Foo opBinary(string op)( in typeof(this) rhv ) if( op == "-=" ) { _bar -= rhv._bar; retur

Re: Compound assignment operators

2010-08-25 Thread Yao G.
Sorry, I sent the message prematurely :( Anyway, here's complete: Is there a way to make compound assignment operators (-=, +=, *= and friends) work with D's operator overload regime? I can't make them work. struct Foo { this( int bar ) { bar = bar; } Foo opBinary(string o

Re: Compound assignment operators

2010-08-25 Thread bearophile
Yao G.: > Is there a way to make this work? Even changing the operator string in > opBinary to "-" doesn't do nothing. Or those kind of operators can't be > overloaded? Try opOpAssign. Bye, bearophile

Re: Compound assignment operators

2010-08-25 Thread Jonathan M Davis
On Wednesday 25 August 2010 22:31:38 Yao G. wrote: > Sorry, I sent the message prematurely :( Anyway, here's complete: > > Is there a way to make compound assignment operators (-=, +=, *= and > friends) work with D's operator overload regime? I can't make them work. Look at http://www.digitalmars

Re: Compound assignment operators

2010-08-25 Thread Yao G.
On Thu, 26 Aug 2010 01:46:02 -0500, Jonathan M Davis wrote: Look at http://www.digitalmars.com/d/2.0/operatoroverloading.html (or even better, TDPL). The correct function would be opOpAssign. I believe that the syntax for += would be opOpAssign!("+")(args) { } - Jonathan M Davis Ha ha