Re: Opinions: The Best and Worst of D (for a lecture/talk I intend to give)
On Wednesday, 9 July 2014 at 19:54:47 UTC, H. S. Teoh via Digitalmars-d-learn wrote: [...] The problem is that the function needs to return int, but given two uints, their difference may be greater than int.max, so simply subtracting them will not work. So the best I can come up with is: int compare2(int x, uint y) { return (x 0) ? -1 : (y int.max) ? -1 : (x - y); } which requires 2 comparisons. Hmm. Diff works for compare(int,int). So how about this: int compare2(int x, uint y) { return (y int.max) ? -1 : (x - cast(int)y); } int compare2(uint x, int y) { return (x int.max) ? -1 : (cast(int)x - y); }
Re: Opinions: The Best and Worst of D (for a lecture/talk I intend to give)
Should of course be: int compare2(uint x, int y) { return (x int.max) ? 1 : (cast(int)x - y); }
Re: How to interact with fortran code
On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote: On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote: I apologize many times for this question, may be this had already been answered somewhere, but considering today the last of my nerve is broken, I can not really find the soution. So I have a D code, which acts as a central manager of all my codes, reads user input, reads files, etc, and based on the file readouts, I would like to pass some variables from the D code to a fortran code, in binary format, perhaps, if such a thing exists, instead of encoding to text/ ASCII first. I would also like to read some (not all) variables back from the fortran code. The Fortran code resides in a subdirectory to the path/to/d/code How to do this? is there a preffered way / easier than system call way to interface D and Fortran code? This must be Fortan code - these are the standard atmospheric chemistry codes. I apologize again if the question is stupid, trust me, today all my nerves are broken. Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D C Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html To expand on that: You don't actually need to write a C glue layer between D and Fortran. All you need to do is make your Fortran functions accessible for C code. As I'm not familiar with Fortran, I don't know how exactly that works, but it could involve telling the compiler to use the right calling convention, and use the right name mangling. On the D side, you can then declare and use the Fortran functions as follows: extern(C) float my_fortran_func(int a, int b); void bar() { writeln(Fortran returned: , my_fortran_func(10, 20)); } Here is an article that describes how to interface with C code from D: http://wiki.dlang.org/Bind_D_to_C
Re: Concatenates int
On 11/07/2014 12:11 a.m., Sean Campbell wrote: i have the ints 4, 7, 0 and 1 how can i Concatenate them into four thousand seven hundred and one. If we talking at compile time definition: int myint = 4_7_0_1; Would work. However I'll assume its at runtime you really want this. I.e. converting a string to an integer. int myint = to!int(4 ~ 7 ~ 0 ~ 1); Now they are not strings, and the positions of 10^ doesn't change then: int myint = (1000 * 4) + (100 * 7) + 1;
Re: Concatenates int
Rikki Cattermole: int myint = to!int(4 ~ 7 ~ 0 ~ 1); And to concatenate them there is join (joiner is not yet usable here, because to!() doesn't yet accept a lazy input, unfortunately). Now they are not strings, and the positions of 10^ doesn't change then: int myint = (1000 * 4) + (100 * 7) + 1; Even if Phobos doesn't yet have a enumerate() function, you can use a iota+zip+reduce to do this. Bye, bearophile
Re: Concatenates int
On 07/10/2014 02:22 PM, Rikki Cattermole wrote: On 11/07/2014 12:11 a.m., Sean Campbell wrote: i have the ints 4, 7, 0 and 1 how can i Concatenate them into four thousand seven hundred and one. If we talking at compile time definition: int myint = 4_7_0_1; Would work. However I'll assume its at runtime you really want this. I.e. converting a string to an integer. int myint = to!int(4 ~ 7 ~ 0 ~ 1); Now they are not strings, and the positions of 10^ doesn't change then: int myint = (1000 * 4) + (100 * 7) + 1; D also has the pow operator, so you can write this as: int i = 4*10^^3 + 7*10^^2 + 0*10^^1 + 1*10^^0;
Re: Introspecting a Module with Traits, allMembers
The others have already given some answers, I just want to point out that the (free) sample chapter of my D book covers this topic too: http://www.packtpub.com/discover-advantages-of-programming-in-d-cookbook/book Scanning a whole module and getting everything out takes a few tricks that I talk about in there.
Re: Concatenates int
perhaps I'd better state what I'm doing. i have an array of 4 bytes and a want to convert them to a 32 bit int and convert the 32 bit int back into a 4 bytes again.
Re: Opinions: The Best and Worst of D (for a lecture/talk I intend to give)
On Monday, 7 July 2014 at 23:47:26 UTC, Aerolite wrote: So, if you would be so kind, give me a bullet list of the aspects of D you believe to be good, awesome, bad, and/or ugly. If you have the time, some code examples wouldn't go amiss either! Try not to go in-depth to weird edge cases - remain general, yet informative. E.g. I consider D's string mixins to be in the 'awesome' category, but its reliance on the GC for large segments of the standard library to be in the 'ugly' category. I'm a big fan of (templated) UFCS. Along with parameterless function calls it goes a long way toward improving readability with its pipe-like flow. And like with all templates, allowing for breaking out common functionality *without* resorting to inheritance. Templates overall are sexy. void main() { import std.stdio; import std.conv; import std.string; import std.range; import core.thread; // values known at compile-time -- CTFE kicks in~ static assert(12345.to!string == 12345); immutable period = 1.seconds + 100.msecs + 100.usecs; writeln(period); Thread.sleep(period); immutable line = fedcba .retro .to!string .toUpper; // wish this worked: typeof(line).is!string; static assert(is(typeof(line) : string)); static assert(line == ABCDEF); }
Re: How to interact with fortran code
On Thursday, 10 July 2014 at 12:12:20 UTC, Marc Schütz wrote: On Wednesday, 9 July 2014 at 15:09:08 UTC, Chris wrote: On Wednesday, 9 July 2014 at 15:00:25 UTC, seany wrote: I apologize many times for this question, may be this had already been answered somewhere, but considering today the last of my nerve is broken, I can not really find the soution. So I have a D code, which acts as a central manager of all my codes, reads user input, reads files, etc, and based on the file readouts, I would like to pass some variables from the D code to a fortran code, in binary format, perhaps, if such a thing exists, instead of encoding to text/ ASCII first. I would also like to read some (not all) variables back from the fortran code. The Fortran code resides in a subdirectory to the path/to/d/code How to do this? is there a preffered way / easier than system call way to interface D and Fortran code? This must be Fortan code - these are the standard atmospheric chemistry codes. I apologize again if the question is stupid, trust me, today all my nerves are broken. Off the top of my head I'd say you could try to interface Fortran and C. Then you could interface D and C, i.e. D C Fortran. http://fortranwiki.org/fortran/show/Generating+C+Interfaces https://gcc.gnu.org/onlinedocs/gcc-3.4.4/g77/C-Interfacing-Tools.html To expand on that: You don't actually need to write a C glue layer between D and Fortran. All you need to do is make your Fortran functions accessible for C code. As I'm not familiar with Fortran, I don't know how exactly that works, but it could involve telling the compiler to use the right calling convention, and use the right name mangling. On the D side, you can then declare and use the Fortran functions as follows: extern(C) float my_fortran_func(int a, int b); void bar() { writeln(Fortran returned: , my_fortran_func(10, 20)); } Here is an article that describes how to interface with C code from D: http://wiki.dlang.org/Bind_D_to_C Cool. I wonder would it be possible to use inline assembly?
Re: Concatenates int
On 11/07/2014 1:18 a.m., Sean Campbell wrote: perhaps I'd better state what I'm doing. i have an array of 4 bytes and a want to convert them to a 32 bit int and convert the 32 bit int back into a 4 bytes again. Small hack I use in Dakka: union RawConvTypes(T) { T value; ubyte[T.sizeof] bytes; ubyte[T.sizeof] opCast() { return bytes; } } auto iRCT = RawConvTypes!int(5); assert(iRCT.bytes == [5, 0, 0, 0]); Can be quite useful for evil conversions.
Sum informations in file....
I have one file with a lot of numeric data... and I need to sum all that data... That is my actual code: module main; import std.stdio; import std.file; import std.conv : to; int main(string[] args) { auto f = File(oi.txt); auto r = f.byLine(); auto tot = 0; foreach(line;r) { if(line[0] == '1') tot += to!int(line[253..266]); } writeln(tot); return 0; } I want to know if have a more better way to make this... maybe using lambda or tamplates
Re: Sum informations in file....
Alexandre: I want to know if have a more better way to make this... maybe using lambda or tamplates Your code is not bad. This is a bit better (untested): void main() { import std.stdio; import std.conv: to; auto lines = oi.txt.File.byLine; int tot = 0; foreach (const line; lines) { if (line[0] == '1') tot += line[253 .. 266].to!int; } tot.writeln; } If you want to write in a mode functional style (untested) (probably it requires the 2.066beta): void main() { import std.stdio, std.algorithm, std.range, std.conv; oi.txt .File .byLine .filter!(line = line[0] == '1') .map!(line = line[253 .. 266].to!int) .sum .writeln; } Bye, bearophile
Re: Sum informations in file....
O, real intresting the mode functional style!!! Like linq! hahah Btw, it's work very well, thansk!!! But, how I can insert an ',' comma to separe the decimal place ? ( the last 2 digits ) I can't find a insert instruction in std.string or std.array On Thursday, 10 July 2014 at 15:01:52 UTC, bearophile wrote: Alexandre: I want to know if have a more better way to make this... maybe using lambda or tamplates Your code is not bad. This is a bit better (untested): void main() { import std.stdio; import std.conv: to; auto lines = oi.txt.File.byLine; int tot = 0; foreach (const line; lines) { if (line[0] == '1') tot += line[253 .. 266].to!int; } tot.writeln; } If you want to write in a mode functional style (untested) (probably it requires the 2.066beta): void main() { import std.stdio, std.algorithm, std.range, std.conv; oi.txt .File .byLine .filter!(line = line[0] == '1') .map!(line = line[253 .. 266].to!int) .sum .writeln; } Bye, bearophile
Re: Concatenates int
On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote: On 11/07/2014 1:18 a.m., Sean Campbell wrote: perhaps I'd better state what I'm doing. i have an array of 4 bytes and a want to convert them to a 32 bit int and convert the 32 bit int back into a 4 bytes again. Small hack I use in Dakka: union RawConvTypes(T) { T value; ubyte[T.sizeof] bytes; ubyte[T.sizeof] opCast() { return bytes; } } auto iRCT = RawConvTypes!int(5); assert(iRCT.bytes == [5, 0, 0, 0]); Can be quite useful for evil conversions. this may sound stupid (new to system programming) but how do you convert to int form ubyte[]
core.exception.InvalidMemoryOperationError
A code I'm working on stops working and starts printing an infinite loop of core.exception.InvalidMemoryOperationError to the command line output. The code is quite complex and the bug seems to present itself almost in random situation so I would like to try to understand the issue better before looking for the wrong line of code hiding somewhere. I've read it might be that something is trying to allocate during a destructor call, but it sounds really strange to me that there's a neverending amount of exceptions being thrown. This is the first exception being thrown (nothing is thrown before the infinite loop begins). Anyone has suggestions/ideas/heard of a similar stuff before?
Re: Concatenates int
Hello, I may have not understood what you actually want to do, but aren't std.bitmanip.peek or std.bitmanip.read what you are looking for ? http://dlang.org/phobos/std_bitmanip.html#.peek
Insert a char in string
I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0');
Re: Insert a char in string
On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); insertInPlace works like this: auto X = 100; auto X1 = X; X.insertInPlace(3, ','); assert(X == 100,); assert(X1 == 100); You can also do this: auto X = 100; auto N = X[0 .. 3] ~ ',' ~ X[3 .. $]; assert(X == 100); assert(N == 100,);
Re: Insert a char in string
Sorry.. I mean: auto X = 100; auto N = X.insertInPlace(3,','); On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0');
Re: Insert a char in string
I used that solution: string InsertComma(string val) { return val[0 .. $-2] ~ , ~ val[$-2 .. $]; } On Thursday, 10 July 2014 at 16:23:44 UTC, John Colvin wrote: On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); insertInPlace works like this: auto X = 100; auto X1 = X; X.insertInPlace(3, ','); assert(X == 100,); assert(X1 == 100); You can also do this: auto X = 100; auto N = X[0 .. 3] ~ ',' ~ X[3 .. $]; assert(X == 100); assert(N == 100,);
Re: Insert a char in string
On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote: Sorry.. I mean: auto X = 100; auto N = X.insertInPlace(3,','); On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); `std.array.insertInPlace` doesn't return anything. In place here means in situ, i.e. it will not create a new string, but insert the new elements into the existing one. This operation may still reallocate, in which case the array slice you're passing in will be updated to point to the new memory. Either use this instead: auto x = 100; auto n = x.dup; n.insertInPlace(3, ','); // or: insertInPlace(n, 3, ','); ... or use slicing and concatenating to construct a new string: auto g = x[0 .. 3] ~ ',' ~ x[3 .. $]; (Side note about style: It's common practice to use lower-case names for variables, upper-case first letters are used to denote types. But of course, that's a matter of taste.)
Re: Concatenates int
On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote: On 11/07/2014 1:18 a.m., Sean Campbell wrote: perhaps I'd better state what I'm doing. i have an array of 4 bytes and a want to convert them to a 32 bit int and convert the 32 bit int back into a 4 bytes again. Small hack I use in Dakka: union RawConvTypes(T) { T value; ubyte[T.sizeof] bytes; ubyte[T.sizeof] opCast() { return bytes; } } auto iRCT = RawConvTypes!int(5); assert(iRCT.bytes == [5, 0, 0, 0]); Can be quite useful for evil conversions. But as I understood the OP, he want's to use the bytes as decimal digits, i.e. assert(my_convert([4,7,0,1]) == 4701); Reinterpret casting will not do this...
Re: Insert a char in string
Oh, I used that letters in upper case, just for a simple sample... On Thursday, 10 July 2014 at 16:32:53 UTC, Marc Schütz wrote: On Thursday, 10 July 2014 at 16:20:29 UTC, Alexandre wrote: Sorry.. I mean: auto X = 100; auto N = X.insertInPlace(3,','); On Thursday, 10 July 2014 at 16:05:51 UTC, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); `std.array.insertInPlace` doesn't return anything. In place here means in situ, i.e. it will not create a new string, but insert the new elements into the existing one. This operation may still reallocate, in which case the array slice you're passing in will be updated to point to the new memory. Either use this instead: auto x = 100; auto n = x.dup; n.insertInPlace(3, ','); // or: insertInPlace(n, 3, ','); ... or use slicing and concatenating to construct a new string: auto g = x[0 .. 3] ~ ',' ~ x[3 .. $]; (Side note about style: It's common practice to use lower-case names for variables, upper-case first letters are used to denote types. But of course, that's a matter of taste.)
Re: Concatenates int
On Thursday, 10 July 2014 at 15:14:21 UTC, Sean Campbell wrote: On Thursday, 10 July 2014 at 13:51:22 UTC, Rikki Cattermole wrote: On 11/07/2014 1:18 a.m., Sean Campbell wrote: perhaps I'd better state what I'm doing. i have an array of 4 bytes and a want to convert them to a 32 bit int and convert the 32 bit int back into a 4 bytes again. Small hack I use in Dakka: union RawConvTypes(T) { T value; ubyte[T.sizeof] bytes; ubyte[T.sizeof] opCast() { return bytes; } } auto iRCT = RawConvTypes!int(5); assert(iRCT.bytes == [5, 0, 0, 0]); Can be quite useful for evil conversions. this may sound stupid (new to system programming) but how do you convert to int form ubyte[] int to ubyte[4]: auto iRCT = RawConvTypes!int(); iRCT.value = 5; writeln(iRCT.bytes); // [5, 0, 0, 0] ubyte[4] to int: auto iRCT = RawConvTypes!int(); iRCT.bytes = [0, 1, 0, 0]; writeln(iRCT.value); // 256
Re: core.exception.InvalidMemoryOperationError
On Thursday, 10 July 2014 at 15:36:53 UTC, francesco cattoglio wrote: A code I'm working on stops working and starts printing an infinite loop of core.exception.InvalidMemoryOperationError to the command line output. The code is quite complex and the bug seems to present itself almost in random situation so I would like to try to understand the issue better before looking for the wrong line of code hiding somewhere. I've read it might be that something is trying to allocate during a destructor call, but it sounds really strange to me that there's a neverending amount of exceptions being thrown. This is the first exception being thrown (nothing is thrown before the infinite loop begins). Anyone has suggestions/ideas/heard of a similar stuff before? I had the same issue with Derelict bindings. Bindings symbols could be already unloaded when a destructor tries to use them.
Re: Sum a lot of numbers...
PS: that is my code: import std.stdio, std.algorithm, std.range, std.conv; string InsertComma(string val) { return val[0 .. $-2] ~ , ~ val[$-2 .. $]; } int main(string[] argv) { auto x = oi.txt .File .byLine .filter!(line = line[0] == '1') .map!(line = line[127 .. 140].to!real) .sum; auto valor = to!string(x); write(InsertComma(valor)); readln(); return 0; } On Thursday, 10 July 2014 at 17:16:01 UTC, Alexandre wrote: Hi :) I need to sum a list of numbers... but, when I calculate the sum of this numbers, I got a simplify representation of sum: 2.97506e+,12 How I can make to get the correctly representation of this number ?
Sum a lot of numbers...
Hi :) I need to sum a list of numbers... but, when I calculate the sum of this numbers, I got a simplify representation of sum: 2.97506e+,12 How I can make to get the correctly representation of this number ?
Re: Sum a lot of numbers...
On Thu, 10 Jul 2014 17:16:00 +, Alexandre wrote: Hi :) I need to sum a list of numbers... but, when I calculate the sum of this numbers, I got a simplify representation of sum: 2.97506e+,12 How I can make to get the correctly representation of this number ? A full decimal representation can be gotten with `format(%f, n);`
Re: Sum a lot of numbers...
On Thu, 10 Jul 2014 17:17:40 +, Justin Whear wrote: On Thu, 10 Jul 2014 17:16:00 +, Alexandre wrote: Hi :) I need to sum a list of numbers... but, when I calculate the sum of this numbers, I got a simplify representation of sum: 2.97506e+,12 How I can make to get the correctly representation of this number ? A full decimal representation can be gotten with `format(%f, n);` And if you need more than the default 6 digits of precision after the decimal, you can use a precision specifier, e.g. for 10 digits: `format (%.10f, n)`
Re: Concatenates int
On Thursday, 10 July 2014 at 15:51:11 UTC, Olivier Pisano wrote: Hello, I may have not understood what you actually want to do, but aren't std.bitmanip.peek or std.bitmanip.read what you are looking for ? http://dlang.org/phobos/std_bitmanip.html#.peek std.bitmanip.peek and std.bitmanip.read will do nicely should of looked more closely if I need to Concatenate ints I'l just use a recursive pow based on length int ConcatInt(int[] anint){ int total = 0; for(int i=0;ianint.length;i++){ total += anint[i]*10^^(anint.length-i-1); } return total; }
Re: Concatenates int
On Thursday, 10 July 2014 at 17:30:17 UTC, Sean Campbell wrote: if I need to Concatenate ints I'l just use a recursive pow based on length int ConcatInt(int[] anint){ int total = 0; for(int i=0;ianint.length;i++){ total += anint[i]*10^^(anint.length-i-1); } return total; } With `foreach_reverse` it looks a little better: ```d int concat_ints(int[] ints) { int result; foreach_reverse (i, int_; ints) { result += int_ * 10 ^^ (ints.length - i - 1); } return result; } ```
Re: Insert a char in string
On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); Do you really want to insert a comma in the string, or do you want to format a number as 100,000,000,000.00?
Re: Insert a char in string
basically format I read a cobol struct file... From pos X to Y I have a money value... but, this value don't have any format.. 0041415 The 15 is the cents... bascally I need to put the ( comma ), we use comma to separate the cents, here in Brazil... On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote: On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); Do you really want to insert a comma in the string, or do you want to format a number as 100,000,000,000.00?
Using enum constant from different modules
Here's a code example: module main; import foo; enum Get = GET; void bar (string a) { assert(a is Get); } void main () { asd(); } module foo; import main; void asd() { bar(Get); } Running the above code will cause an assert error in the function bar. But if I move the function asd into the main module and completely skip the foo module the assert passes. I don't know if I'm thinking completely wrong here but this seems like a bug to me. -- /Jacob Carlborg
Re: Insert a char in string
On 07/10/2014 09:58 PM, Alexandre wrote: basically format I read a cobol struct file... From pos X to Y I have a money value... but, this value don't have any format.. 0041415 The 15 is the cents... bascally I need to put the ( comma ), we use comma to separate the cents, here in Brazil... On Thursday, 10 July 2014 at 19:33:15 UTC, simendsjo wrote: On 07/10/2014 06:05 PM, Alexandre wrote: I have a string X and I need to insert a char in that string... auto X = 100; And I need to inser a ',' in position 3 of this string..., I try to use the array.insertInPlace, but, not work... I try this: auto X = 100; auto N = X.insertInPlace(1,'0'); Do you really want to insert a comma in the string, or do you want to format a number as 100,000,000,000.00? I'm not sure what you're trying to do though. Do you need to fix the file by adding a comma at appropriate places? Or read it into D and write it to the console with your currency format? This is one way of reading in the values using slices and std.conv: import std.stdio, std.conv; void main() { immutable input = 0041415; double amount = input[0..$-2].to!double(); amount += input[$-2..$].to!double() / 100; writeln(amount); }
Re: Using enum constant from different modules
On Thursday, 10 July 2014 at 20:27:39 UTC, Jacob Carlborg wrote: Here's a code example: module main; import foo; enum Get = GET; void bar (string a) { assert(a is Get); } void main () { asd(); } module foo; import main; void asd() { bar(Get); } Running the above code will cause an assert error in the function bar. But if I move the function asd into the main module and completely skip the foo module the assert passes. I don't know if I'm thinking completely wrong here but this seems like a bug to me. No, this is equivalent to: void bar (string a) { assert(a is GET); } void asd() { bar(GET); } Enums behave as if their values are copy-n-pasted everywhere they are used (you probably know that). The compiler probably conflates the two identical strings when they're in the same module. This is safe for immutable data. I'm sure there's something in the spec about it...
Re: Using enum constant from different modules
On Thursday, 10 July 2014 at 20:27:39 UTC, Jacob Carlborg wrote: Here's a code example: module main; import foo; enum Get = GET; void bar (string a) { assert(a is Get); } void main () { asd(); } module foo; import main; void asd() { bar(Get); } Running the above code will cause an assert error in the function bar. But if I move the function asd into the main module and completely skip the foo module the assert passes. I don't know if I'm thinking completely wrong here but this seems like a bug to me. I don't think this is a bug. Remember that enums have copy-paste semantics. So, this is the same as comparing literals from different modules. Apparently, in the same module, a duplicate string literal is optimized out. But that's not done across the module boundary. I'd guess that's because of the separate compilation model.
Re: Using enum constant from different modules
On 07/10/2014 10:47 PM, Marc Schütz schue...@gmx.net wrote: On Thursday, 10 July 2014 at 20:27:39 UTC, Jacob Carlborg wrote: Here's a code example: module main; import foo; enum Get = GET; void bar (string a) { assert(a is Get); } void main () { asd(); } module foo; import main; void asd() { bar(Get); } Running the above code will cause an assert error in the function bar. But if I move the function asd into the main module and completely skip the foo module the assert passes. I don't know if I'm thinking completely wrong here but this seems like a bug to me. No, this is equivalent to: void bar (string a) { assert(a is GET); } void asd() { bar(GET); } Enums behave as if their values are copy-n-pasted everywhere they are used (you probably know that). The compiler probably conflates the two identical strings when they're in the same module. This is safe for immutable data. I'm sure there's something in the spec about it... Strings behaves a bit odd with is(). The following passes: import std.stdio; void f(string a, string b) { assert(a is b); // also true } void main() { string a = aoeu; string b = aoeu; assert(a is b); // true f(a, b); writeln(passed); } changing a and b to enum gives the same results.
Re: Using enum constant from different modules
On Thursday, 10 July 2014 at 20:59:17 UTC, simendsjo wrote: Strings behaves a bit odd with is(). The following passes: import std.stdio; void f(string a, string b) { assert(a is b); // also true } void main() { string a = aoeu; string b = aoeu; assert(a is b); // true f(a, b); writeln(passed); } ```d import std.stdio; void f(string a, string b) { writeln(a: , a.ptr, , b: , b.ptr); assert(a is b); // also true } void main() { string a = aoeu; string b = aoeu; writeln(a: , a.ptr, , b: , b.ptr); assert(a is b); // true f(a, b); writeln(passed); } ``` Output: ``` a: 4210A0, b: 4210A0 a: 4210A0, b: 4210A0 passed ``` Seems legit to me.
Setting dates
I've been trying to set a date for my program (a small struct): import std.datetime; auto date = cast(DateTime)Clock.currTime(); setDate(date.day, date.month, date.year); Problem is that day month are not integers. And date.day.to!int doesn't work either.
Re: Using enum constant from different modules
On 07/11/2014 01:08 AM, sigod wrote: On Thursday, 10 July 2014 at 20:59:17 UTC, simendsjo wrote: Strings behaves a bit odd with is(). The following passes: import std.stdio; void f(string a, string b) { assert(a is b); // also true } void main() { string a = aoeu; string b = aoeu; assert(a is b); // true f(a, b); writeln(passed); } ```d import std.stdio; void f(string a, string b) { writeln(a: , a.ptr, , b: , b.ptr); assert(a is b); // also true } void main() { string a = aoeu; string b = aoeu; writeln(a: , a.ptr, , b: , b.ptr); assert(a is b); // true f(a, b); writeln(passed); } ``` Output: ``` a: 4210A0, b: 4210A0 a: 4210A0, b: 4210A0 passed ``` Seems legit to me. I forgot to check for compiler optimizations (even without -O). immutable(int)[] a = [1]; immutable(int)[] b = [1]; assert(a is b); // fails as .ptr is different. So it looks like string literals is cached by the compiler and reused. Changing aoeu to 10.to!string for instance breaks this optimization. But the fact that immutable(char)[] behaves different from immutable(int)[] is a bit strange.