Re: They are not the same

2014-04-04 Thread Meta
On Saturday, 5 April 2014 at 01:57:59 UTC, Meta wrote: When I put a println inside both functions, they both print out [2, 1, 0]. Shouldn't the first print [0, 1, 2] while the second prints [2, 1, 0]? No, sorry, I was mistaken about what was going on.

Re: They are not the same

2014-04-04 Thread Meta
On Saturday, 5 April 2014 at 01:28:06 UTC, bearophile wrote: Can you spot the difference between foo1 and foo2? import std.algorithm: map; import std.range: iota; void foo1(in int[] a, in int[] b) pure { int[] r; foreach (immutable i; 0 .. a.length) r ~= (i % 2) ? a[i] : b[i];

Re: two questions on enums

2014-04-04 Thread Eric
By using the interface, it also forces the user to include all of the attributes of each pin such as direction, max load, DC current, etc. Since class type enums are references, they are light, - and they should be immutable - so they are thread safe aslo. I'm not sure how you're using your

They are not the same

2014-04-04 Thread bearophile
Can you spot the difference between foo1 and foo2? import std.algorithm: map; import std.range: iota; void foo1(in int[] a, in int[] b) pure { int[] r; foreach (immutable i; 0 .. a.length) r ~= (i % 2) ? a[i] : b[i]; } void foo2(in int[] a, in int[] b) pure { int[] r; f

Re: two questions on enums

2014-04-04 Thread Chris Williams
On Thursday, 3 April 2014 at 23:16:14 UTC, Eric wrote: Okay - I'm new to D, and I'm comming from a java background. Suppose you are designing an API, and you want the user to supply arguments as an enum. But the user needs to define the enum, so how can the API know in advance what enum to re

templates, enums, CT reflection: compiler bug or not?

2014-04-04 Thread captaindet
i stumbled upon something strange while wondering around meta/codegen-lands. it took me almost a week to reduce it to the following test case. i have no clue what is going on, whether i have entered bug territory or just encountered my own limitations mind you, i have 2 issues with the cod

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread Olivier Grant
Sorry Steven, I hadn't seen your answer. Thanks for the extra information. O. On Friday, 4 April 2014 at 20:42:32 UTC, Steven Schveighoffer wrote: On Fri, 04 Apr 2014 16:35:57 -0400, Olivier Grant wrote: That is one responsive D community :) Thank you to all three of you for these quick

Re: Manually-allocated memory and maximum array capacity

2014-04-04 Thread Artur Skawina
On 04/05/14 00:54, Joseph Rushton Wakeling wrote: > Hello all, > > If we change the length of a dynamic array using the normal GC-based methods, > e.g. by setting the array's .length property, we find that the array's > capacity typically does not simply equal the length, but some greater value;

Re: Manually-allocated memory and maximum array capacity

2014-04-04 Thread bearophile
Joseph Rushton Wakeling: Question: is there a comparable phenomenon for memory that is manually allocated using malloc? Manually allocated memory can over-allocate, but not geometrically as arrays do. Take a look at the difference between core.memory.extend and core.memory.realloc. Bye,

Manually-allocated memory and maximum array capacity

2014-04-04 Thread Joseph Rushton Wakeling
Hello all, If we change the length of a dynamic array using the normal GC-based methods, e.g. by setting the array's .length property, we find that the array's capacity typically does not simply equal the length, but some greater value; there is excess allocation. Question: is there a compar

A simplification error when calculating array lengths

2014-04-04 Thread Ali Çehreli
(This was in C and probably a common mistake that I haven't experienced until today.) tldr; The following two expressions are not equivalent: a)length - 1 - length / 2 b)length / 2 - 1 I was trying to write a recursive function similar to binary search: - Process the middle elemen

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread Olivier Grant
Yes, that definitely seems like a good start. To be honest, I not familiar enough yet with how you are suppose to organise your source code in D. Is there any agreed-on naming convention when it comes to member variables to avoid that kind of name clash ? Thanks, O. On Friday, 4 April 2014

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread bearophile
Olivier Grant: Is there any way to hide Splicer.array to the outside world? If your splice is inside another module, and you tag the array field with private, you will receive an error: auto splice( size_t N, R )( R range ) if(isInputRange!R) { struct Splicer { private R arr

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread Steven Schveighoffer
On Fri, 04 Apr 2014 16:35:57 -0400, Olivier Grant wrote: That is one responsive D community :) Thank you to all three of you for these quick answers. I was really scratching my head on that one. This shows that UFCS can be quite dangerous. Is there any way to hide Splicer.array to the ou

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread Olivier Grant
That is one responsive D community :) Thank you to all three of you for these quick answers. I was really scratching my head on that one. This shows that UFCS can be quite dangerous. Is there any way to hide Splicer.array to the outside world? It just seems that it would be very easy to break

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread Tobias Pankrath
On Friday, 4 April 2014 at 20:19:53 UTC, Olivier Grant wrote: Hi, I've started using D as a scripting language at work and for personal projects as a means to learn the language and I've bumped into a case where I would like to iterate a slice by a certain number of elements at a time such th

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread anonymous
On Friday, 4 April 2014 at 20:19:53 UTC, Olivier Grant wrote: import std.stdio; import std.range; import std.array; auto splice( size_t N, R )( R range ) if(isInputRange!R) { struct Splicer { R array; @property bool empty( ) const { return 0 == array.length; }

Re: std.array.array seems to return flatten copy of input

2014-04-04 Thread anonymous
On Friday, 4 April 2014 at 20:19:53 UTC, Olivier Grant wrote: iterate a slice by a certain number of elements at a time such that : foreach(s; [1,2,3,4].splice!(2)) { writeln(s); } would print : [1,2] [3,4] First of all, is there such a function in D's standard library as I didn't seem

std.array.array seems to return flatten copy of input

2014-04-04 Thread Olivier Grant
Hi, I've started using D as a scripting language at work and for personal projects as a means to learn the language and I've bumped into a case where I would like to iterate a slice by a certain number of elements at a time such that : foreach(s; [1,2,3,4].splice!(2)) { writeln(s); } wou

Re: How to hand in a closure variable

2014-04-04 Thread Jesse Phillips
On Friday, 4 April 2014 at 15:13:25 UTC, Bienlein wrote: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; But I'm happy enough with the solution through a delegate. What bearophile said, or: immutable int b = {i

Re: list in spam blacklist? [OT]

2014-04-04 Thread Brad Roberts
Once every few years spamhaus 'cleans up' their white lists with no notifications. I've already initiated the re-removal. Sigh. I hate spamhaus, they're a pain in every mail sender's backside. On 4/4/14, 10:25 AM, Hugo Florentino wrote: Hi, IP 173.45.241.208 (slice-1.puremagic.com) seems to

Re: porting GCC macro to D

2014-04-04 Thread Ali Çehreli
On 04/04/2014 08:42 AM, ketmar wrote: You can hack around that limitation if it's acceptable to use a temporary variable: sure, and i can rewrite the whole call, passing destination variable as template argument. but this looks ugly. A regular function would work too: void func0 () { } int f

list in spam blacklist? [OT]

2014-04-04 Thread Hugo Florentino
Hi, IP 173.45.241.208 (slice-1.puremagic.com) seems to be blacklisted in zen.spamhaus.org Please check for a possible misuse of the server, or in case of a false positive report to spamhaus.org Regards, Hugo

Re: How do I obtain the default hash of a user-defined struct

2014-04-04 Thread bearophile
H. S. Teoh: This means that the hash of MyKey is computed based on its binary representation, disregarding the contents of any array (and other reference) fields. This will certainly break AA's. I'm almost certain this has already been reported as a bug, but I vaguely remember someone mentio

Re: How do I obtain the default hash of a user-defined struct

2014-04-04 Thread H. S. Teoh
On Fri, Apr 04, 2014 at 04:48:52PM +, dnspies wrote: > On Thursday, 3 April 2014 at 23:01:27 UTC, Steven Schveighoffer wrote: > >On Thu, 03 Apr 2014 17:42:16 -0400, bearophile > >wrote: > > > > > >>I have filed this big problem four years ago or more. > > > >Bug report? > > > >-Steve > > This

Re: How do I obtain the default hash of a user-defined struct

2014-04-04 Thread dnspies
On Thursday, 3 April 2014 at 23:01:27 UTC, Steven Schveighoffer wrote: On Thu, 03 Apr 2014 17:42:16 -0400, bearophile wrote: I have filed this big problem four years ago or more. Bug report? -Steve This is the closest I could find: https://d.puremagic.com/issues/show_bug.cgi?id=11025

Re: Why defining alias and not auto when using a template?

2014-04-04 Thread Frustrated
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote: Hello, I took some code snippet from some sample D code and modified it a bit: template TCopy(T, V) { private int i = 2; void copy(out T to, out V to2, T from) { to = from; to2 = from; writeln("i: ", i); } }

Re: porting GCC macro to D

2014-04-04 Thread ketmar
You can hack around that limitation if it's acceptable to use a temporary variable: sure, and i can rewrite the whole call, passing destination variable as template argument. but this looks ugly. thanx for the answer anyway (and bearophile too). Unfortunately, you cannot use template mixins fo

Re: porting GCC macro to D

2014-04-04 Thread Marc Schütz
On Friday, 4 April 2014 at 13:22:39 UTC, ketmar wrote: i have some C code like this: void func0 (void) { … } int func1 (void) { … return smth+42; } #define MYMACRO() ({ func0(); func1(); }) int v; … v = MYMACRO()-16; v = mixin(MYMACRO())-16; and have the same effect as in C code: all MYMA

Re: How to hand in a closure variable

2014-04-04 Thread bearophile
Bienlein: What I was actually looking for was how to get this to work: immutable int b = if(1 == 1) { return 123; } else { return 456; }; immutable b = (1 == 1) ? 123 : 456; Bye, bearophile

Re: How to hand in a closure variable

2014-04-04 Thread Bienlein
On Friday, 4 April 2014 at 13:53:33 UTC, bearophile wrote: If your D function has one argument, you have to give it one argument, even if it doesn't have a visible name and it's unused. Ah! Admittedly, I though it's the return type .. So this works now: immutable int b = () { if(1 ==

Re: Why defining alias and not auto when using a template?

2014-04-04 Thread Steven Schveighoffer
On Fri, 04 Apr 2014 09:35:57 -0400, Bienlein wrote: "auto" is used to declare an instance, or an object. "alias" is used to declare a name. What you are currently doing is saying "the function TCopy!(int, int) can now be refered to as myCopy". You aren't actually creating any data. All r

Re: How to hand in a closure variable

2014-04-04 Thread bearophile
Bienlein: Whereas this does not compile: immutable int b = (int) { if(1 == 1) { return 123; } else { return 456; } }(); // line x However, this does compile and displays correctly 123: immutable int b = (int) { if(1 == 1) {

Re: Why defining alias and not auto when using a template?

2014-04-04 Thread anonymous
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote: template TCopy(T, V) { [...] } On Friday, 4 April 2014 at 13:35:58 UTC, Bienlein wrote: auto myCopy = new TCopy!(int, int); alias myCopy = new TCopy!(int, int); Neither nor compiles now. How can? Seems to me a template is no

Re: How to hand in a closure variable

2014-04-04 Thread Bienlein
Thanks so far. I have another one, though. Not trying to tease people, I really don't know ;-). This compiles and runs: immutable int a = (int val) { if(1 == 1) { return val; } else { return 456; } }(123); writeln(a); Whereas th

Re: When this will be freed?

2014-04-04 Thread Steven Schveighoffer
On Fri, 04 Apr 2014 09:25:49 -0400, Marc Schütz wrote: On Wednesday, 2 April 2014 at 16:51:57 UTC, Benjamin Thaut wrote: Am 02.04.2014 17:57, schrieb Andrea Fontana: I mean: if it is an exported function (of a shared library) what happens? There's no reference kept anywhere (in D). So if I'm

Re: Why defining alias and not auto when using a template?

2014-04-04 Thread Bienlein
"auto" is used to declare an instance, or an object. "alias" is used to declare a name. What you are currently doing is saying "the function TCopy!(int, int) can now be refered to as myCopy". You aren't actually creating any data. All right, thanks. Then I create an instance: auto myCopy =

Re: porting GCC macro to D

2014-04-04 Thread bearophile
ketmar: and have the same effect as in C code: all MYMACRO content (this can be almost anything) *always* inlined. Define a function. And in D there is no way to require a sure inline. Some people have asked for a __forceinline but Andrei&Walter don't seem sufficiently interested. Bye, bea

Re: Why defining alias and not auto when using a template?

2014-04-04 Thread monarch_dodra
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote: Hello, I took some code snippet from some sample D code and modified it a bit: template TCopy(T, V) { private int i = 2; void copy(out T to, out V to2, T from) { to = from; to2 = from; writeln("i: ", i); } }

Re: When this will be freed?

2014-04-04 Thread Marc Schütz
On Wednesday, 2 April 2014 at 16:51:57 UTC, Benjamin Thaut wrote: Am 02.04.2014 17:57, schrieb Andrea Fontana: I mean: if it is an exported function (of a shared library) what happens? There's no reference kept anywhere (in D). So if I'm right it could be freed immediatly by GC. Right? If y

Re: Extracting Params of Templated Type

2014-04-04 Thread Meta
On Friday, 4 April 2014 at 07:23:51 UTC, Andrej Mitrovic wrote: On 4/4/14, Meta wrote: alias TemplateArgs(T: Foo!U, U) = U; It's also in std.traits, TemplateArgsOf, which is more complete. I thought I remembered that being in std.traits, but I couldn't find it after quickly skimming. I mus

Why defining alias and not auto when using a template?

2014-04-04 Thread Bienlein
Hello, I took some code snippet from some sample D code and modified it a bit: template TCopy(T, V) { private int i = 2; void copy(out T to, out V to2, T from) { to = from; to2 = from; writeln("i: ", i); } } void main(string[] args) { int x = 2; int

porting GCC macro to D

2014-04-04 Thread ketmar
i have some C code like this: void func0 (void) { … } int func1 (void) { … return smth+42; } #define MYMACRO() ({ func0(); func1(); }) int v; … v = MYMACRO()-16; what it does? it uses GCC extension "statement expression" which allows to place almost arbitrary C operator block inside any ex

Re: How can I specify a location to write the code coverage files generated with '-cov'?

2014-04-04 Thread Saurabh Das
On Friday, 4 April 2014 at 08:52:09 UTC, Daniel Murphy wrote: On Friday, 4 April 2014 at 04:10:51 UTC, Saurabh Das wrote: Hello, Say I compile a program with: dmd -unittest -debug -cov test.d Then, when I run ./test, a file 'test.lst' is generated in the current working directory. Is there a

Re: How can I specify a location to write the code coverage files generated with '-cov'?

2014-04-04 Thread Daniel Murphy
On Friday, 4 April 2014 at 04:10:51 UTC, Saurabh Das wrote: Hello, Say I compile a program with: dmd -unittest -debug -cov test.d Then, when I run ./test, a file 'test.lst' is generated in the current working directory. Is there a way in which I can instruct the file to be created in a separ

Re: Extracting Params of Templated Type

2014-04-04 Thread Andrej Mitrovic
On 4/4/14, Meta wrote: > alias TemplateArgs(T: Foo!U, U) = U; It's also in std.traits, TemplateArgsOf, which is more complete.