Re: It is a bug?
On Wednesday, 30 July 2014 at 16:14:56 UTC, Jesse Phillips wrote: On Wednesday, 30 July 2014 at 07:08:17 UTC, Kozzi11 wrote: #main.d: import m.f; class A { //class main.A member m is not accessible //mixin(t!(typeof(this), m)); void m() {}; //here is ok //mixin(t!(typeof(this), m)); } The compiler is trying to construct type A. The first thing it sees is a need to get the members of the class. What members are in the class? I don't know I'm trying to mix one in right now! Yes, this make sense. But why it is not a problem when 't' template is in the same file.
Re: pointer array?
V Wed, 30 Jul 2014 14:33:51 + seany via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: In Ali's excllent book, somehow one thing has escaped my attention, and that it the mentioning of pointer arrays. Can pointers of any type of pointed variable be inserted in an int array? Using to!(int) perhaps? If not directly, then what else would achieve the same effect? It depends on pointer size, for eg, on 64bit system with 64bit pointers something like this should works: long[] arr = (cast(long*)pointersArray.ptr)[0 .. pointersArray.length]; or long[] arr = cast(long[])pointersArray; // but I am not sure if this is OK
Re: Member access of __gshared global object
V Thu, 31 Jul 2014 02:03:35 + Puming via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: Hi, I'm writing this global Config class, with an AA member: ```d module my.config; class Config { Command[string] commands; } __gshared Config CONFIG; ``` and initialize it in another module: ```d module my.app; import my.config; void main() { CONFIG = new Config(); CONFIG.commands[bye] = new Command(...); // add commands } ``` This is OK. But when I use a local variable to hold the commands AA: ``` auto cmds = CONFIG.commands; cmds[list] = new Command(...); ``` The command list is not added. I guess what happened here was that `cmds` is a threadlocal variable, so the compiler somehow copied the CONFIG.commands. My questions are: 1. Are AAs reference type? if so, why does the compiler copy it? 2. How do I reference a member of __gshared global objects? can you post code somewhere? I try it and it works for me. module main; import std.stdio; import config; void main(string[] args) { CONFIG = new Config(); CONFIG.commands[bye] = yep; auto cmds = CONFIG.commands; cmds[list] = smt; writeln(CONFIG.commands); // Lets the user press Return before program returns stdin.readln(); } module config; class Config { string[string] commands; this() { // Constructor code } } __gshared Config CONFIG;
Re: Split class declaration and definition
V Thu, 31 Jul 2014 13:26:38 + Kagamin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Thursday, 31 July 2014 at 12:02:22 UTC, Kozzi11 wrote: module m; @someUda class C { void someFun(); } @someUda class D { void anotherFun(); } mixin(generateFunDefForClassesWithSomeUda!m); This is usually done by generating functions in the classes directly. class C { mixin Generate!C; } Yes this is how I do it now.
Re: A significant performance difference
V Sun, 31 Aug 2014 10:55:31 + bearophile via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: This is C++ code that solves one Euler problem: -- #include stdio.h #include map const unsigned int H = 9, W = 12; const int g[6][3] = {{7, 0, H - 3}, {1 + (1 H) + (1 (2 * H)), 0, H - 1}, {3 + (1 H), 0, H - 2}, {3 + (2 H), 0, H - 2}, {1 + (1 H) + (2 H), 0, H - 2}, {1 + (1 H) + (1 (H - 1)), 1, H - 1}}; int main() { unsigned long long p, i, k; unsigned int j, l; std::mapunsigned int, unsigned long long x, y; x[0] = 1; for (i = 0; i W; ++i) { y.clear(); while (!x.empty()) { j = x.begin()-first; p = x.begin()-second; x.erase(x.begin()); for (k = 0; k H; ++k) if ((j (1 k)) == 0) break; if (k == H) y[j H] += p; else for (l = 0; l 6; ++l) if (k = g[l][1] k = g[l][2]) if ((j (g[l][0] k)) == 0) x[j + (g[l][0] k)] += p; } x = y; } printf(%lld\n, y[0]); return 0; } -- I have translated it to D like this (I know in D there are nicer ways to write it, but I have tried to keep the look of the code as much similar as possible to the C++ code): -- import core.stdc.stdio; const uint H = 9, W = 12; const uint[3][6] g = [[7, 0, H - 3], [1 + (1 H) + (1 (2 * H)), 0, H - 1], [3 + (1 H), 0, H - 2], [3 + (2 H), 0, H - 2], [1 + (1 H) + (2 H), 0, H - 2], [1 + (1 H) + (1 (H - 1)), 1, H - 1]]; int main() { ulong p, i, k; uint j, l; ulong[uint] x, y; x[0] = 1; for (i = 0; i W; ++i) { y = null; while (x.length) { j = x.byKey.front; p = x.byValue.front; x.remove(cast(int)j); for (k = 0; k H; ++k) if ((j (1 k)) == 0) break; if (k == H) y[j H] += p; else for (l = 0; l 6; ++l) if (k = g[l][1] k = g[l][2]) if ((j (g[l][0] k)) == 0) x[j + (g[l][0] k)] += p; } x = y; } printf(%lld\n, y[0]); return 0; } -- The C++ code is much faster than the D code (I see the D code 30+ times slower with dmd and about like 20 times with ldc2). One difference between the C++ and D code is that the C++ map uses a search tree (red-black probably), while the D code uses a hash. To test that algorithmic difference, if I replace the map in the C++ code with a std::unordered_map (C++11): #include unordered_map ... std::unordered_mapunsigned int, unsigned long long x, y; then the run-time increases (more than two times) but it's still much faster than the D code. Is it possible to fix the D code to increase its performance (there are associative array libraries for D, but I have not tried them in this program). Bye, bearophile I think main problem is in calling delegates (x.byKey.front and x.byValue.front;). If I change x.byValue.front with x[j], It makes program a lot faster
Re: A significant performance difference
V Mon, 1 Sep 2014 12:38:52 +0300 ketmar via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Mon, 01 Sep 2014 09:22:50 + bearophile via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: In theory the best solution is to improve the performance of the byKey.front and byValue.front idioms. i found that slowdown is from _aaRange(), not from delegates. the following code is slow even w/o delegate creation: import core.stdc.stdio; ref K firstKey(T : V[K], V, K)(T aa) @property { return *cast(K*)_aaRangeFrontKey(_aaRange(cast(void*)aa)); } ref V firstVal(T : V[K], V, K)(T aa) @property { return *cast(V*)_aaRangeFrontValue(_aaRange(cast(void*)aa)); } int main() { long[int] aa; for (int i = 0; i 5; i++) aa[i] = i; long total = 0; while (aa.length) { //int k = aa.byKey.front; int k = aa.firstKey; //long v = aa.byValue.front; long v = aa.firstVal; aa.remove(k); total += k + v; } printf(%lld\n, total); return 0; } seems that we need two more hooks for AAs: `_aaFrontKey()` and `_aaFrontValue()`. x.pop() sounds nicer :-) ah, sure. i'm not very good in inventing names. ;-) Yep, I found out something similar. with this ugly code for (i = 0; i W; ++i) { y = null; while (x.length) { foreach(lj, lp ; x) { j = lj; p = lp; x.remove(cast(int)j); break; } ... } The problem is with searching first element
Re: alias this cast
V Thu, 11 Sep 2014 11:40:05 + andre via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: Hi, I am 80% sure, the failing assertion is correct but please have a look. No it is not assert(cast(A)cast(C)b); // this is OK b is B so it does not know about having alias to A; Second assertion fails. Kind regards André class A{} class B{} class C : B { A a; alias a this; this() { a = new A(); } } void main() { B b = new C(); // OK assert(cast(C)b); // fails assert(cast(A)b); }
Re: std.algorithm.reduce on an array of structs
On Thursday, 11 September 2014 at 13:06:05 UTC, Colin wrote: I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) = a b); writefln(%s, min1); // prints 1 as expected Thing[] ar2 = [Thing(1), Thing(2), Thing(4)]; auto min2 = ar2.reduce!((a,b) = a.x b.x); // - Wont Compile writefln(%s, min2); } The line with Wont Compile on it has this error message: /usr/include/dmd/phobos/std/algorithm.d(770): Error: cannot implicitly convert expression (__lambda2(result, front(_param_1))) of type bool to Thing /usr/include/dmd/phobos/std/algorithm.d(791): Error: template instance t.main.reduce!((a, b) = a.x b.x).reduce!(Thing, Thing[]) error instantiating t.d(16):instantiated from here: reduce!(Thing[]) Any idea what I'm doing wrong here? To me, the operation on ar2 should be pretty much identical to ar1, except for the use of the struct. You are try to put uint to Thing. This is corect version: import std.stdio; import std.algorithm; struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) = a b); writefln(%s, min1); // prints 1 as expected Thing[] ar2 = [Thing(1), Thing(2), Thing(4)]; auto min2 = ar2.reduce!((a,b) = a.x b.x ? a : b); // - Wont Compile writefln(%s, min2); }
Re: std.algorithm.reduce on an array of structs
On Thursday, 11 September 2014 at 14:18:31 UTC, Colin wrote: Ah ok. I get it. Thanks daniel! a quiet better version: import std.stdio; import std.algorithm; struct Thing { uint x; alias x this; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) = min(a,b)); writefln(%s, min1); Thing[] ar2 = [Thing(1), Thing(2), Thing(4)]; auto min2 = ar2.reduce!((a,b) = min(a,b)); writefln(%s, min2); }
Re: std.algorithm.reduce on an array of structs
On Thursday, 11 September 2014 at 14:39:53 UTC, Daniel Kozak wrote: On Thursday, 11 September 2014 at 14:18:31 UTC, Colin wrote: Ah ok. I get it. Thanks daniel! a quiet better version: import std.stdio; import std.algorithm; struct Thing { uint x; alias x this; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) = min(a,b)); writefln(%s, min1); Thing[] ar2 = [Thing(1), Thing(2), Thing(4)]; auto min2 = ar2.reduce!((a,b) = min(a,b)); writefln(%s, min2); } s/quiet/quite/
Re: std.algorithm.reduce on an array of structs
V Thu, 11 Sep 2014 14:49:02 + bearophile via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; } alias minimum = reduce!min; void main() { immutable ar1 = [10, 20, 30, 40, 50]; ar1.minimum.writeln; immutable ar2 = [Thing(10), Thing(20), Thing(40)]; ar2.minimum.writeln; } Bye, bearophile Yep, this look the most idiomatic :). Why there is no phobos function for minimum of array(range)?
Re: std.algorithm.reduce on an array of structs
On Thursday, 11 September 2014 at 14:56:00 UTC, Daniel Kozak via Digitalmars-d-learn wrote: V Thu, 11 Sep 2014 14:49:02 + bearophile via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; } alias minimum = reduce!min; void main() { immutable ar1 = [10, 20, 30, 40, 50]; ar1.minimum.writeln; immutable ar2 = [Thing(10), Thing(20), Thing(40)]; ar2.minimum.writeln; } Bye, bearophile Yep, this look the most idiomatic :). Why there is no phobos function for minimum of array(range)? or use alias minimum = reduce!a b; ;)
Re: std.algorithm.reduce on an array of structs
On Thursday, 11 September 2014 at 15:07:03 UTC, Daniel Kozak wrote: or use alias minimum = reduce!a b; ;) ok this one does not work
Re: Is there a function that reads the entire contents of a std.stdio.File?
V Tue, 16 Sep 2014 14:37:05 + Jay via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: all the functions/methods i've come across so far deal with either streams or just file names (like std.file.read) and there doesn't seem to be a way to wrap a std.stdio.File in a stream (or is there?). i need a function that takes a std.stdio.File and returns a string or byte array. You can use rawRead: http://dlang.org/phobos/std_stdio.html#.File.rawRead
parallel foreach hangs
this code never end import std.stdio; import std.file; import std.parallelism : parallel; import std.algorithm : filter; void main(string[] args) { foreach(d; parallel(args[1 .. $], 1)) { auto phpFiles = filter!`endsWith(a.name,.php)`(dirEntries(d,SpanMode.depth)); writeln(phpFiles); } }
Re: Does D has C#'s string.Empty?
V Thu, 25 Sep 2014 05:29:36 + AsmMan via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: Does D has C#'s string.Empty? string.init
Re: curl and proxy
On Friday, 3 October 2014 at 11:13:11 UTC, Marc Schütz wrote: On Friday, 3 October 2014 at 10:53:27 UTC, Marc Schütz wrote: On Friday, 3 October 2014 at 04:57:28 UTC, AntonSotov wrote: auto http = HTTP(dlang.org); http.onReceive = (ubyte[] data) { writeln(cast(string) (data)); return data.length; }; http.proxy = 192.168.111.111; http.proxyPort = 1788; WHAT HERE ? http.perform(); // how to make Сurl authorize on a proxy. I specify proxyUser and proxyPassword? I think there's currently no way. curl provides this as an option (CurlOption.proxyuserpwd): curl.set(CurlOption.proxyuserpwd, myuser:mypasswd); But unfortunately the `curl` struct is a private member of `HTTP`... https://github.com/D-Programming-Language/phobos/pull/2581 If you're able to build Phobos, could you give this PR a try? I cannot test it myself, because I don't know of a proxy requiring authentication. Works OK for me Thanks
Re: Best way to add slash to tail of the path
Dne Thu, 27 Nov 2014 21:20:24 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): take a second look then. ;-) you'll find `buildPath()` here too. Not better: string foo = D:/code/txtDownloader; writeln(foo); foo = foo.buildPath; foo ~= config.txt; writeln(foo); Running .\txtdownloader.exe D:/code/txtDownloader D:/code/txtDownloaderconfig.txt -- need: D:/code/txtDownloader/config.txt what about: string foo = D:/code/txtDownloader; writeln(foo); foo = buildPath(foo, config.txt); writeln(foo);
Re: Best way to add slash to tail of the path
try first few sentences and looked at the example ;) Dne Thu, 27 Nov 2014 21:42:31 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): Could you quote for me part of docs where it's written? I really can't understand about what you are taking. -- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Re: function app.download (string[] links) is not callableusing argument types (string, string)
Dne Thu, 27 Nov 2014 18:39:09 +0100 Suliman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): Is there any way to detect where collision was occurred? Yes, read error description it has been app.download I guess -- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Re: Error: cannot return non-void from void function
On Thursday, 27 November 2014 at 17:22:36 UTC, Suliman wrote: ah, that's it! as spec says, D determines function return value from the first 'return' statement it seen. in your case this is `return;`, so function return type is determined to be `void`. if you doing `auto` functions, try to arrange your code so the first `return` returning the actual value. besides, your code is wrong anyway, 'cause you can't have function that returns both nothing and something. your first `return;` should either return something, or must be changed to throwing some exception. How I can terminate program? First return I used to terminate app if config file is exists. You can use this: auto parseConfig() { string txtlinks = buildPath(getcwd,notexist); if(exists(txtlinks)) { auto lines = File(txtlinks, r).byLine; return lines; } writeln(Can't find input file with list of links.); return typeof(return)(); } it works somehow, but it is not good way how to do it. Other way is use exit if(!exists(txtlinks)) { import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); } But best way is use throw exception if(!exists(txtlinks)) { throw new Exception(Can't find input file with list of links.); } and catch it somewhere from calling side
Re: Error: cannot return non-void from void function
Dne Thu, 27 Nov 2014 22:21:52 +0100 ketmar via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): On Thu, 27 Nov 2014 21:14:57 + Daniel Kozak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: import core.runtime; import std.c.process; writeln(Can't find input file with list of links.); Runtime.terminate(); exit(1); please-please-please don't teach people that! using such features requiring deep understanding of how D runtime works, and how it interacts with C runtime, with GC, with stack objects and so on. it's better to now show people bad samples instead of telling them don't do what i just wrote. ;-) I know, I just can't help myself :).
Re: How to initialise array of ubytes?
Dne Sat, 29 Nov 2014 21:10:41 +0100 Paul via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsal(a): I'm trying to do this: ubyte[MAPSIZE][MAPSIZE] map = 1; but it doesn't work and I can't seem to cast the value to a ubyte (which looks rather ugly and out of place in D anyway). Is there a way to do this other than using a couple of loops? Cheers Paul http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361 -- Vytvořeno poštovní aplikací Opery: http://www.opera.com/mail/
Re: How to initialise array of ubytes?
On Saturday, 29 November 2014 at 20:45:34 UTC, bearophile wrote: Daniel Kozak: http://stackoverflow.com/questions/24600796/d-set-default-value-for-a-struct-member-which-is-a-multidimensional-static-arr/24754361#24754361 Do you also know why the simplest syntax doesn't work? Can't it be implemented and added to the D language? Bye, bearophile I don't know. But this works too: module main; import std.stdio; int[5][5] p; static this() { p = 1; } void main() { writeln(p); }
Re: math.log() benchmark of first 1 billion int using std.parallelism
I run Arch Linux on my PC. I compiled D programs using dmd-2.066 and used no compile arguments (dmd prog.d) You should try use some arguments -O -release -inline -noboundscheck and maybe try use gdc or ldc should help with performance can you post your code in all languages somewhere? I like to try it on my machine :)
Re: math.log() benchmark of first 1 billion int using std.parallelism
On Monday, 22 December 2014 at 10:35:52 UTC, Daniel Kozak via Digitalmars-d-learn wrote: I run Arch Linux on my PC. I compiled D programs using dmd-2.066 and used no compile arguments (dmd prog.d) You should try use some arguments -O -release -inline -noboundscheck and maybe try use gdc or ldc should help with performance can you post your code in all languages somewhere? I like to try it on my machine :) Btw. try use C log function, maybe it would be faster: import core.stdc.math;
Re: math.log() benchmark of first 1 billion int using std.parallelism
That's very different to my results. I see no important difference between ldc and dmd when using std.math, but when using core.stdc.math ldc halves its time where dmd only manages to get to ~80% What CPU do you have? On my Intel Core i3 I have similar experience as Iov Gherman, but on my Amd FX4200 I have same results as you. Seems std.math.log is not good for my AMD CPU :)
Re: math.log() benchmark of first 1 billion int using std.parallelism
On Tuesday, 23 December 2014 at 10:39:13 UTC, Iov Gherman wrote: These multi-threaded benchmarks can be very sensitive to their environment, you should try running it with nice -20 and do multiple passes to get a vague idea of the variability in the result. Also, it's important to minimise the number of other running processes. I did not use the nice parameter but I always ran them multiple times and choose the average time. My system has very few running processes, minimalist ArchLinux with Xfce4 so I don't think the running processes are affecting in any way my tests. And what about single threaded version? Btw. One reason why DMD is faster is because it use fyl2x X87 instruction here is version for others compilers: import std.math, std.stdio, std.datetime; enum SIZE = 100_000_000; version(GNU) { real mylog(double x) pure nothrow { real result; double y = LN2; asm { fldl %2\n fldl %1\n fyl2x : =t (result) : m (x), m (y); } return result; } } else { real mylog(double x) pure nothrow { return yl2x(x, LN2); } } void main() { auto t1 = Clock.currTime(); auto logs = new double[SIZE]; foreach (i; 0 .. SIZE) { logs[i] = mylog(i + 1.0); } auto t2 = Clock.currTime(); writeln(time: , (t2 - t1)); } But it is faster only on all Intel CPU, but on one of my AMD it is slower than core.stdc.log
Re: math.log() benchmark of first 1 billion int using std.parallelism
On Tuesday, 23 December 2014 at 10:20:04 UTC, Iov Gherman wrote: That's very different to my results. I see no important difference between ldc and dmd when using std.math, but when using core.stdc.math ldc halves its time where dmd only manages to get to ~80% I checked again today and the results are interesting, on my pc I don't see any difference between std.math and core.stdc.math with ldc. Here are the results with all compilers. - with std.math: dmd: 4 secs, 878 ms ldc: 5 secs, 650 ms gdc: 9 secs, 161 ms - with core.stdc.math: dmd: 5 secs, 991 ms ldc: 5 secs, 572 ms gdc: 7 secs, 957 ms Btw. I just noticed small issue with D vs. java, you start messure in D before allocation, but in case of Java after allocation
Re: math.log() benchmark of first 1 billion int using std.parallelism
On Tuesday, 23 December 2014 at 12:31:47 UTC, Iov Gherman wrote: Btw. I just noticed small issue with D vs. java, you start messure in D before allocation, but in case of Java after allocation Here is the java result for parallel processing after moving the start time as the first line in main. Still best result: 4 secs, 50 ms average Java: Exec time: 6 secs, 421 ms LDC (-O3 -release -mcpu=native -singleobj -inline -boundscheck=off) time: 5 secs, 321 ms, 877 μs, and 2 hnsecs GDC(-O3 -frelease -march=native -finline -fno-bounds-check) time: 5 secs, 237 ms, 453 μs, and 7 hnsecs DMD(-O -release -inline -noboundscheck) time: 5 secs, 107 ms, 931 μs, and 3 hnsecs So all d compilers beat Java in my case: but I have made some change in D version: import std.parallelism, std.math, std.stdio, std.datetime; import core.memory; enum XMS = 3*1024*1024*1024; //3GB version(GNU) { real mylog(double x) pure nothrow { double result; double y = LN2; asm { fldl %2\n fldl %1\n fyl2x\n : =t (result) : m (x), m (y); } return result; } } else { real mylog(double x) pure nothrow { return yl2x(x, LN2); } } void main() { GC.reserve(XMS); auto t1 = Clock.currTime(); auto logs = new double[1_000_000_000]; foreach(i, ref elem; taskPool.parallel(logs, 200)) { elem = mylog(i + 1.0); } auto t2 = Clock.currTime(); writeln(time: , (t2 - t1)); }
Re: Is D's GC.calloc and C's memset played the same role?
FrankLike via Digitalmars-d-learn píše v Út 23. 12. 2014 v 15:37 +: Today,I meet a question:get all processes names. --C++ CODE- #include stdafx.h #include windows.h #include stdio.h//C standard I/O #include tlhelp32.h int _tmain(int argc, _TCHAR* argv[]) { HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(hProcessSnap==INVALID_HANDLE_VALUE) { _tprintf(_T(CreateToolhelp32Snapshot error!\n)); return -1; } PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); BOOL bMore=Process32First(hProcessSnap,pe32); int i=0; _tprintf(_T(PID\t thread nums \t name \n)); while(bMore) { bMore=Process32Next(hProcessSnap,pe32); _tprintf(_T(%u\t),pe32.th32ProcessID); _tprintf(_T(%u\t),pe32.cntThreads); _tprintf(_T(%s\n),pe32.szExeFile); i++; } CloseHandle(hProcessSnap); _tprintf(_T(Count:%d\n),i); return 0; } D code-- import std.stdio; import std.string; import core.sys.windows.windows; import core.memory; import win32.tlhelp32; void main() { HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if(hProcessSnap is null) { writeln(CreateToolhelp32Snapshot error!\n); return ; } PROCESSENTRY32* pe32 = cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof); pe32.dwSize = PROCESSENTRY32.sizeof; bool bMore=cast(bool)Process32First(hProcessSnap,pe32); int i=0; writeln(PID\t thread nums\t name \n); while(bMore) { bMore=cast(bool)Process32Next(hProcessSnap,pe32); string s = cast(string)pe32.szExeFile; auto a = s.indexOf('\0'); if(a =0) writeln(\t,pe32.th32ProcessID,\t,pe32.cntThreads,\t,s[0..a]); i++; } CloseHandle(hProcessSnap); writeln(format(count:%d,i)); return ; } ---end-- you will find the different: D: PROCESSENTRY32* pe32 = cast(PROCESSENTRY32*)GC.calloc(PROCESSENTRY32.sizeof); C++:PROCESSENTRY32 pe32; GC.calloc means: memset ?! calloc means alloc cleared memory same as malloc but clear all bits to zero
Re: Call of rmdir in destructor causes InvalidMemoryOperationError
Timo Gransch via Digitalmars-d-learn píše v Čt 01. 01. 2015 v 16:14 +0100: Hi, I have a class which unzips an archive into a temporary directory below the system temp folder. I want to delete this temporary directory in the class's destructor, but when I call rmdir there, I get an core.exception.InvalidMemoryOperationError@(0) The effect is not limited to this special case. Whenever I call rmdir in the destructor, no matter for which directory, I get the same error. Environment: DMD v2.066.1 (from D-Apt) on Ubuntu 14.10 (32 and 64 bit). Sample code: // --- module main; import std.stdio; import std.file; import std.path; class RmdirTest { private string sTempDir; this(string sInstanceName) { writeln(Constructor called); sTempDir=tempDir() ~ dirSeparator ~ sInstanceName; mkdir (sTempDir); } ~this() { writeln(Destructor called); if (sTempDir !is null) { rmdir(sTempDir); } } } void main(string[] args) { RmdirTest rmDirTest=new RmdirTest(123); } // --- Console output is: Constructor called Destructor called core.exception.InvalidMemoryOperationError@(0) The directory /tmp/123 is created, but not deleted. When I change the line (sTempDir !is null) to ((sTempDir !is null) (exists(sTempDir)) (isDir(sTempDir))), the exception is thrown already on this line, so obviously the problem also applies to other file functions like exists and isDir. Is there any solution for this? Thanks and best regards, Timo You shoud not use destructor for this operation. In D there is no guarantee that class destructor will be called. So you can probablly use struct instead of class or add some method and called it explicitly when needed
Re: Scoped external function declaration
On Thursday, 1 January 2015 at 17:51:46 UTC, novice2 wrote: I want to use external or C function. It used only one time from one D function. I want do declare C function inside D function. I don't want to declare C function in global scope. Is my wish correct? Reduced code: extern (C) int getch(); void main() { getch(); } //compiled OK void main() { extern (C) int getch(); getch(); } //Error 42: Symbol Undefined __D4test4mainFZ5getchUZi you can use local import cfun.d: module cfun; extern (C) int getc(); main.d: module main; void main() { import cfun; getc(); }
What exactly shared means?
I always think that shared should be use to make variable global across threads (similar to __gshared) with some synchronize protection. But this code doesn't work (app is stuck on _aaGetX or _aaRehash ): shared double[size_t] logsA; void main() { auto logs = new double[1_000_000]; foreach(i, ref elem; parallel(logs, 4)) { elem = log(i + 1.0); logsA[i]= elem; } } But when I add synchronized block it is OK: shared double[size_t] logsA; void main() { auto logs = new double[1_000_000]; foreach(i, ref elem; parallel(logs, 4)) { elem = log(i + 1.0); synchronized { logsA[i]= elem; } } }
Re: static class vs. static struct
On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote: For several times I've met struct(or static struct) usage in Phobos for singleton pattern implementation. Unfortunately now i can remember only core.runtime.Runtime. So I've got a question. Why do Phobos guys use struct or static struct for or singleton pattern implementation? Why don't use static final class for this purpose? I do not think this is a singleton pattern (no instance). I see it much more like namespace in case of core.runtime.Runtime. And yes static final class could do that too but struct looks better than final class and you can disable this on structs
Re: static class vs. static struct
On Tuesday, 27 January 2015 at 09:36:49 UTC, Daniel Kozak wrote: On Tuesday, 27 January 2015 at 09:01:39 UTC, ref2401 wrote: For several times I've met struct(or static struct) usage in Phobos for singleton pattern implementation. Unfortunately now i can remember only core.runtime.Runtime. So I've got a question. Why do Phobos guys use struct or static struct for or singleton pattern implementation? Why don't use static final class for this purpose? I do not think this is a singleton pattern (no instance). I see it much more like namespace in case of core.runtime.Runtime. And yes static final class could do that too but struct looks better than final class and you can disable this on structs import std.stdio; import std.conv; struct S { @disable this(); } final class C { } void main() { writeln(C.sizeof); writeln(S.sizeof); }
Re: Why can't functions and struct types have the same name?
On Monday, 26 January 2015 at 11:15:26 UTC, Joakim wrote: Right now, any attempt to have symbols with the same name errors out, regardless of how they're used. This caused a problem for me because I'm trying to use a third-party C library that defines a struct type called socket and my code calls that library and some networking modules from druntime. Since core.sys.posix.sys.socket happens to contain a function called socket also, dmd gets confused when I try to create a socket* and use it in my code. I would think it'd know I can't do that with a function name. Is such symbol disambiguation a convenience that just isn't implemented yet or something that can't/won't be done? Right now, I had to go through and selectively import all 14 symbols I needed from the 3 druntime modules that publicly import core.sys.posix.sys.socket, so that the function socket isn't imported. Seems like a pain that can be mitigated by a smarter compiler. You can use static import, or alias to solve this issue
Re: For those ready to take the challenge
Vladimir Panteleev via Digitalmars-d-learn píše v So 10. 01. 2015 v 07:42 +: On Saturday, 10 January 2015 at 02:10:04 UTC, Jesse Phillips wrote: On Friday, 9 January 2015 at 13:50:29 UTC, eles wrote: https://codegolf.stackexchange.com/questions/44278/debunking-stroustrups-debunking-of-the-myth-c-is-for-large-complicated-pro Link to answer in D: http://codegolf.stackexchange.com/a/44417/13362 I think byLine is not necessary. By default . will not match line breaks. One statement solution: import std.net.curl, std.stdio; import std.algorithm, std.regex; void main() { get(http://www.stroustrup.com/C++.html;) .matchAll(`a.*?href=(.*)`) .map!(m = m[1]) .each!writeln(); } Requires Phobos PR#2024 ;) Oh here is it, I was looking for each. I think it is allready in a phobos but I can not find. Now I know why :D
Re: Parameterized enum does not work
On Friday, 9 January 2015 at 06:17:53 UTC, Andre wrote: Hi, Should following coding work? string lpad(ubyte length, long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } enum lpad14(long n) = lpad(14, n); void main() { lpad14(123); } There is following error from dmd: source\app.d(12): Error: template app.lpad14 cannot deduce function from argumen t types !()(int), candidates are: source\app.d(8):app.lpad14(long n) Kind regards André What are you trying to do?
Re: Parameterized enum does not work
On Friday, 9 January 2015 at 07:50:53 UTC, Daniel Kozak wrote: On Friday, 9 January 2015 at 06:17:53 UTC, Andre wrote: Hi, Should following coding work? string lpad(ubyte length, long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } enum lpad14(long n) = lpad(14, n); void main() { lpad14(123); } There is following error from dmd: source\app.d(12): Error: template app.lpad14 cannot deduce function from argumen t types !()(int), candidates are: source\app.d(8):app.lpad14(long n) Kind regards André What are you trying to do? OK I probably see it now :): import std.stdio; string lpad(ubyte length, long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } enum lpad14(long n) = lpad(14, n); void main() { writeln(lpad14!(123)); }
Re: Parameterized enum does not work
On Friday, 9 January 2015 at 07:52:50 UTC, Daniel Kozak wrote: On Friday, 9 January 2015 at 07:50:53 UTC, Daniel Kozak wrote: On Friday, 9 January 2015 at 06:17:53 UTC, Andre wrote: Hi, Should following coding work? string lpad(ubyte length, long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } enum lpad14(long n) = lpad(14, n); void main() { lpad14(123); } There is following error from dmd: source\app.d(12): Error: template app.lpad14 cannot deduce function from argumen t types !()(int), candidates are: source\app.d(8):app.lpad14(long n) Kind regards André What are you trying to do? OK I probably see it now :): import std.stdio; string lpad(ubyte length, long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } enum lpad14(long n) = lpad(14, n); void main() { writeln(lpad14!(123)); } However this is only for compile time, if you want runtime version you need something like this: import std.stdio; string lpad(ubyte length)(long n) { import std.string: rightJustify; import std.conv: to; return rightJustify(to!string(n), length, '0'); } alias lpad14 = lpad!14; void main() { writeln(lpad14(123)); }
Re: Endless static this call when used a thread in it
On Tuesday, 13 January 2015 at 14:02:45 UTC, Daniel Kozák via Digitalmars-d-learn wrote: V Tue, 13 Jan 2015 13:56:05 + tcak via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Tuesday, 13 January 2015 at 13:53:11 UTC, tcak wrote: I have written the following code: test.d == import core.thread; import std.stdio; void threadFunc(){ writeln(Thread func); } public static this(){ auto t = new Thread( threadFunc ); t.start(); writeln(Static init); } void main(){ writeln(End of main); } run == rdmd test.d result == Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Static init Thread func Sta... Is this normal, what's happening? When I defined static init with shared public shared static this() it works normal now. But it doesn't explain above issue. What's the relation between a new thread and a module's initialiser? I am not sure but my guess is static this needs to be called before anything else in module so when you try call threadFunc it looks if static this has been called and finished which is not true so it call it again And here is better explanation http://dlang.org/module.html#staticorder Static constructors are code that gets executed to initialize a module or a class before the main() function gets called. ... Static constructors and static destructors run on thread local data, and are run whenever threads are created or destroyed.
Re: final methods by default
On Fri, 20 Mar 2015 22:11:51 + weaselcat via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Friday, 20 March 2015 at 14:25:22 UTC, ref2401 wrote: Why aren't methods of class final by default? history use final class, it should devirtualize all methods. see: https://github.com/D-Programming-Language/dmd/pull/4427 Yes, but you can not extend final class. Ok you can still use UFCS but it is not elegand solution.
Re: final methods by default
On Fri, 20 Mar 2015 16:27:04 -0700 Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Friday, March 20, 2015 23:53:14 Daniel Kozak via Digitalmars-d-learn wrote: On Fri, 20 Mar 2015 22:11:51 + weaselcat via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Friday, 20 March 2015 at 14:25:22 UTC, ref2401 wrote: Why aren't methods of class final by default? history use final class, it should devirtualize all methods. see: https://github.com/D-Programming-Language/dmd/pull/4427 Yes, but you can not extend final class. Ok you can still use UFCS but it is not elegand solution. Then you can just do class Foo { final: // methods... } or class Foo { final { // methods... } } And even if you couldn't do that, you could always mark each function with final individually. Yes I know that and use it. Not often because I use struct and templates so I need to marks methods as final occasionally ;-)
Re: Unicode exception raise when replacing underscore with space
On Tuesday, 13 January 2015 at 20:30:16 UTC, Nordlöw wrote: On Tuesday, 13 January 2015 at 13:01:56 UTC, Daniel Kozák via Digitalmars-d-learn wrote: What do I need to do/add to avoid auto-decoding here? std.array.replace(x, `_`, ` `); Thanks! What about adding See alsos in the docs that relate these two with respect to auto-decoding? I am not sure, it doesn`t exactly do the same. And to be fair std.array.replace use internaly std.algorithm.find which use in some scenario auto-decoding. So to be sure no autodecoding occured you must used something like that: string x = some_text; auto res = std.array.replace(cast(byte[])x, [byte('_')], [byte(' ')]); writeln(cast(string)res);
Re: Static function template
On Thursday, 7 May 2015 at 10:19:44 UTC, Lemonfiend wrote: Is it not possible to have a static function template with the same name as the non-static version? struct S { int i; auto foo(T)(int j) { i=j; } static auto foo(T)(int j) { S s; s.foo!T(j); return s; } } void main() { auto s = S.foo!bool(1); } Error: need 'this' for 'foo' of type '(int j)' Another thinks which is wierd is than on 2.066 it prints: test.d(17): Error: need 'this' for 'foo' of type 'pure nothrow @nogc @safe void(int j)' So it seems auto deduction for attributes does not work anymore :( (in this case I guess)
Re: Static function template
On Thursday, 7 May 2015 at 11:18:17 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 11:15:02 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 11:08:50 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:46:19 + Lemonfiend via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Thursday, 7 May 2015 at 10:43:28 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 10:39:09 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:33:44 + Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: struct S { int i; auto foo2(T)(int j) { i=j; } static S foo(T)(int j) { S s; s.foo2!T(j); return s; } } void main() { auto s = S.foo!bool(1); } As I said, it is not bug. It is OK. There is no way how you can distinguish between static and non static methods or even field in some cases. e.g.: import std.stdio; struct S { string foo = Please select me?; string foo() { return (No, select me?); }; static string foo() { return (I am better than the otters :D?); }; } void main() { auto s = S(); writeln(s.foo); } Well it's clear to me now why it shouldn't work. However, the error msg is not clear on the problem. Imo it should give a conflict error like in your previous example. That would make it clear what's happened/allowed. Yep, I think you are right even this example make useless and wrong error message: struct S { static S foo(T)(int j) { S s; return s; } static S foo(T)(int j) { S s; return s; } } void main() { auto s = S.foo!bool(1); } test.d(15): Error: need 'this' for 'foo' of type '(int j)' // WTF? btw. it is a regresion from 2.067 on 2.066 and before it makes much better error. Even for OP code when is modified (static must be declared before non static one) it have a better error msg. test.d(6): Error: test.S.foo called with argument types (int) matches both: test.d(4): test.S.foo!bool.foo(int j) and: test.d(10): test.S.foo!bool.foo(int j) test.d(18): Error: template instance test.S.foo!bool error instantiating But only when static one is declared before non static one, so even on 2.066 it was not ideal. So I think we should open two issues, probably one for regression and one for enhancment OK both are regressions, cause on dmd 2.063 it is much much better test.d(19): Error: template test.S.foo matches more than one template declaration, test.d(3):foo(T)(int j) and test.d(8):foo(T)(int j) test.d(19): Error: need 'this' for 'foo' of type 'pure nothrow @safe void(int j)' https://issues.dlang.org/show_bug.cgi?id=14554
Re: Static function template
On Thursday, 7 May 2015 at 11:15:02 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 11:08:50 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:46:19 + Lemonfiend via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Thursday, 7 May 2015 at 10:43:28 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 10:39:09 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:33:44 + Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: struct S { int i; auto foo2(T)(int j) { i=j; } static S foo(T)(int j) { S s; s.foo2!T(j); return s; } } void main() { auto s = S.foo!bool(1); } As I said, it is not bug. It is OK. There is no way how you can distinguish between static and non static methods or even field in some cases. e.g.: import std.stdio; struct S { string foo = Please select me?; string foo() { return (No, select me?); }; static string foo() { return (I am better than the otters :D?); }; } void main() { auto s = S(); writeln(s.foo); } Well it's clear to me now why it shouldn't work. However, the error msg is not clear on the problem. Imo it should give a conflict error like in your previous example. That would make it clear what's happened/allowed. Yep, I think you are right even this example make useless and wrong error message: struct S { static S foo(T)(int j) { S s; return s; } static S foo(T)(int j) { S s; return s; } } void main() { auto s = S.foo!bool(1); } test.d(15): Error: need 'this' for 'foo' of type '(int j)' // WTF? btw. it is a regresion from 2.067 on 2.066 and before it makes much better error. Even for OP code when is modified (static must be declared before non static one) it have a better error msg. test.d(6): Error: test.S.foo called with argument types (int) matches both: test.d(4): test.S.foo!bool.foo(int j) and: test.d(10): test.S.foo!bool.foo(int j) test.d(18): Error: template instance test.S.foo!bool error instantiating But only when static one is declared before non static one, so even on 2.066 it was not ideal. So I think we should open two issues, probably one for regression and one for enhancment OK both are regressions, cause on dmd 2.063 it is much much better test.d(19): Error: template test.S.foo matches more than one template declaration, test.d(3):foo(T)(int j) and test.d(8):foo(T)(int j) test.d(19): Error: need 'this' for 'foo' of type 'pure nothrow @safe void(int j)'
Re: Static function template
On Thursday, 7 May 2015 at 11:08:50 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:46:19 + Lemonfiend via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Thursday, 7 May 2015 at 10:43:28 UTC, Daniel Kozak wrote: On Thursday, 7 May 2015 at 10:39:09 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:33:44 + Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: struct S { int i; auto foo2(T)(int j) { i=j; } static S foo(T)(int j) { S s; s.foo2!T(j); return s; } } void main() { auto s = S.foo!bool(1); } As I said, it is not bug. It is OK. There is no way how you can distinguish between static and non static methods or even field in some cases. e.g.: import std.stdio; struct S { string foo = Please select me?; string foo() { return (No, select me?); }; static string foo() { return (I am better than the otters :D?); }; } void main() { auto s = S(); writeln(s.foo); } Well it's clear to me now why it shouldn't work. However, the error msg is not clear on the problem. Imo it should give a conflict error like in your previous example. That would make it clear what's happened/allowed. Yep, I think you are right even this example make useless and wrong error message: struct S { static S foo(T)(int j) { S s; return s; } static S foo(T)(int j) { S s; return s; } } void main() { auto s = S.foo!bool(1); } test.d(15): Error: need 'this' for 'foo' of type '(int j)' // WTF? btw. it is a regresion from 2.067 on 2.066 and before it makes much better error. Even for OP code when is modified (static must be declared before non static one) it have a better error msg. test.d(6): Error: test.S.foo called with argument types (int) matches both: test.d(4): test.S.foo!bool.foo(int j) and: test.d(10): test.S.foo!bool.foo(int j) test.d(18): Error: template instance test.S.foo!bool error instantiating But only when static one is declared before non static one, so even on 2.066 it was not ideal. So I think we should open two issues, probably one for regression and one for enhancment
Re: Static function template
On Thursday, 7 May 2015 at 10:39:09 UTC, Daniel Kozák wrote: On Thu, 07 May 2015 10:33:44 + Vadim Lopatin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: struct S { int i; auto foo2(T)(int j) { i=j; } static S foo(T)(int j) { S s; s.foo2!T(j); return s; } } void main() { auto s = S.foo!bool(1); } As I said, it is not bug. It is OK. There is no way how you can distinguish between static and non static methods or even field in some cases. e.g.: import std.stdio; struct S { string foo = Please select me?; string foo() { return (No, select me?); }; static string foo() { return (I am better than the otters :D?); }; } void main() { auto s = S(); writeln(s.foo); }
Re: How to create a mutable array of strings?
On Sun, 17 May 2015 09:06:38 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi, It seems to me, or D do not create mutable array of strings? How to create a mutable equivalent of a string array? - string[] s = [foo, bar]; // s[1][1] = 't'; // immutable expression s[1][1] auto s = [foo.dup, bar.dup];
Re: How to create a mutable array of strings?
On Sun, 17 May 2015 09:06:38 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi, It seems to me, or D do not create mutable array of strings? How to create a mutable equivalent of a string array? - string[] s = [foo, bar]; // s[1][1] = 't'; // immutable expression s[1][1] or you can use cast if you are sure thats what you really need: auto s = [cast(char[])foo, cast(char[])bar]; or auto s = cast(char[][])[foo, bar];
Re: How to create a mutable array of strings?
On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote: On Sun, 17 May 2015 09:06:38 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi, It seems to me, or D do not create mutable array of strings? How to create a mutable equivalent of a string array? - string[] s = [foo, bar]; // s[1][1] = 't'; // immutable expression s[1][1] or you can use cast if you are sure thats what you really need: auto s = [cast(char[])foo, cast(char[])bar]; or auto s = cast(char[][])[foo, bar]; But I am unsure if this will work in case where immutable data will occupied read only memory.
Re: How to create a mutable array of strings?
On Sunday, 17 May 2015 at 09:21:58 UTC, Marc Schütz wrote: On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote: On Sun, 17 May 2015 09:06:38 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi, It seems to me, or D do not create mutable array of strings? How to create a mutable equivalent of a string array? - string[] s = [foo, bar]; // s[1][1] = 't'; // immutable expression s[1][1] or you can use cast if you are sure thats what you really need: auto s = [cast(char[])foo, cast(char[])bar]; or auto s = cast(char[][])[foo, bar]; That's not a good idea. I haven't checked, but this will likely segfault on mutation Yep, you are right, shame on me :)
Re: What wrong?
On Friday, 15 May 2015 at 09:20:32 UTC, Gary Willoughby wrote: On Friday, 15 May 2015 at 07:51:29 UTC, thedeemon wrote: On Saturday, 2 May 2015 at 02:51:52 UTC, Fyodor Ustinov wrote: Simple code: http://pastebin.com/raw.php?i=7jVeMFXQ What I'm doing wrong? Try using class instead of struct. Last time I played with std.concurrency it used Variants to store the messages, so when something bigger than a little basic value or a reference, like a class object, is sent it behaves unpredictably: can crash or shit garbage. Trying to send structs larger than ~20 bytes usually caused problems. Please raise a bugzilla issue for this. this commit cause the issue: https://github.com/D-Programming-Language/phobos/commit/45fda72192ff5b878ebe915db0ffb9f6504cca8f but it is probably just a trigger not a real cause.
Re: ICE?
On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: Is this error an ICE? I think so, because I see the internal filename, but I'm not sure. Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] https://github.com/D-Programming-Language/dmd/pull/4667
Re: How to create a mutable array of strings?
On Sunday, 17 May 2015 at 09:20:17 UTC, Dennis Ritchie wrote: On Sunday, 17 May 2015 at 09:18:15 UTC, Daniel Kozak wrote: auto s = cast(char[][])[foo, bar]; Thanks. This version I was completely satisfied. So maybe this one would be ok with you too :) auto s = to!(char[][])([foo, bar]);
Re: How to create a mutable array of strings?
On Sun, 17 May 2015 09:39:21 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I remembered code Ali Çereli. It really helped: http://forum.dlang.org/thread/ulhtlyxxclihaseef...@forum.dlang.org#post-mihl6m:241che:241:40digitalmars.com - import std.stdio, std.traits, std.range, std.algorithm; auto deepDup(A)(A arr) if (isArray!A) { static if (isArray!(ElementType!A)) { return arr.map!(a = a.deepDup).array; } else { return arr.dup; } } void main() { auto s = [foo, bar].deepDup; s[1][1] = 't'; writeln(s); } - http://rextester.com/QBFH12695 P.S. Need to enable deepDup in Phobos. allready there: auto s = [foo, bar].map!a.dup.array; :)
Re: ICE?
On Sunday, 17 May 2015 at 09:59:41 UTC, Daniel Kozak wrote: On Sun, 17 May 2015 09:33:27 + Namespace via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Sunday, 17 May 2015 at 09:30:16 UTC, Gary Willoughby wrote: On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: Is this error an ICE? I think so, because I see the internal filename, but I'm not sure. Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] Have you got a code sample to reproduce this? Of course: void main() { import core.stdc.stdlib : malloc, free; auto ptr = cast(char[]) malloc(42); } I guess it should be: auto ptr = cast(char*)malloc(42)[0 .. 42]; or this if you want ptr to be char[] and not a pointer to char: auto ptr = (cast(char*)malloc(42))[0 .. 42]; or auto ptr = cast(char[])malloc(42)[0 .. 42];
Re: ICE?
On Sun, 17 May 2015 10:17:42 + anonymous via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Sunday, 17 May 2015 at 10:09:11 UTC, Daniel Kozak wrote: On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: [...] Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] I would say this is not an ICE just normal error message. e2ir: shouldn't be there, though. Yep
Re: ICE?
On Sun, 17 May 2015 09:33:27 + Namespace via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Sunday, 17 May 2015 at 09:30:16 UTC, Gary Willoughby wrote: On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: Is this error an ICE? I think so, because I see the internal filename, but I'm not sure. Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] Have you got a code sample to reproduce this? Of course: void main() { import core.stdc.stdlib : malloc, free; auto ptr = cast(char[]) malloc(42); } I guess it should be: auto ptr = cast(char*)malloc(42)[0 .. 42];
Re: How to create a mutable array of strings?
On Sunday, 17 May 2015 at 09:57:05 UTC, Daniel Kozak wrote: On Sun, 17 May 2015 09:39:21 + Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I remembered code Ali Çereli. It really helped: http://forum.dlang.org/thread/ulhtlyxxclihaseef...@forum.dlang.org#post-mihl6m:241che:241:40digitalmars.com - import std.stdio, std.traits, std.range, std.algorithm; auto deepDup(A)(A arr) if (isArray!A) { static if (isArray!(ElementType!A)) { return arr.map!(a = a.deepDup).array; } else { return arr.dup; } } void main() { auto s = [foo, bar].deepDup; s[1][1] = 't'; writeln(s); } - http://rextester.com/QBFH12695 P.S. Need to enable deepDup in Phobos. allready there: auto s = [foo, bar].map!a.dup.array; :) Ouch ignore this one :D
Re: ICE?
On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: Is this error an ICE? I think so, because I see the internal filename, but I'm not sure. Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] I would say this is not an ICE just normal error message.
Re: ICE?
On Sun, 17 May 2015 10:36:33 + Namespace via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Sunday, 17 May 2015 at 09:59:41 UTC, Daniel Kozak wrote: On Sun, 17 May 2015 09:33:27 + Namespace via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Sunday, 17 May 2015 at 09:30:16 UTC, Gary Willoughby wrote: On Sunday, 17 May 2015 at 09:25:33 UTC, Namespace wrote: Is this error an ICE? I think so, because I see the internal filename, but I'm not sure. Error: e2ir: cannot cast malloc(length * 8u) of type void* to type char[] Have you got a code sample to reproduce this? Of course: void main() { import core.stdc.stdlib : malloc, free; auto ptr = cast(char[]) malloc(42); } I guess it should be: auto ptr = cast(char*)malloc(42)[0 .. 42]; That would work, but I did it on purpose. I wanted to test whether such dangerous code compiles or whether the compiler is smart enough and hits the alarm. I see. for eg.: void main() { import core.stdc.stdlib : malloc, free; static struct arr { size_t length; void* ptr; } auto ptr = cast(arr) malloc(42); } prints: test2.d(9): Error: cannot cast expression malloc(42LU) of type void* to arr which makes sense, even e2ir text is not here.
Re: Printing an std.container.Array
On Thu, 16 Apr 2015 19:55:52 + Bayan Rafeh via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Executing this code: import std.container.array; import std.stdio; int main() { writeln(Array!int([1, 2])); return 0; } outputs the following: Array!int(RefCounted!(Payload, cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0))) The strange thing is that this works fine: import std.container.array; import std.stdio; int main() { writeln(Array!int([1, 2])[0..$]); return 0; } [1, 2] How am I supposed to interpret this? https://github.com/D-Programming-Language/phobos/pull/2875
Re: Printing an std.container.Array
On Thu, 16 Apr 2015 13:05:48 -0700 H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Thu, Apr 16, 2015 at 07:55:52PM +, Bayan Rafeh via Digitalmars-d-learn wrote: Executing this code: import std.container.array; import std.stdio; int main() { writeln(Array!int([1, 2])); return 0; } outputs the following: Array!int(RefCounted!(Payload, cast(RefCountedAutoInitialize)0)(RefCountedStore(B694B0))) The strange thing is that this works fine: import std.container.array; import std.stdio; int main() { writeln(Array!int([1, 2])[0..$]); return 0; } [1, 2] How am I supposed to interpret this? Try slicing the Array before passing it to writeln? writeln(Array!int([1, 2])[]); Basically, there is a distinction between a container and a range that spans the items in a container. The conventional syntax for getting a range over a container's contents is the slicing operator []. T Yep, but problem is almost no one expect this, or know this. We definitely should do better.
Re: Converting void* to D array
On Wednesday, 15 April 2015 at 04:43:39 UTC, Daniel Kozák wrote: On Wed, 15 Apr 2015 04:24:20 + Craig Dillabaugh via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi. I want to call a C library function that returns a data buffer as a void*. How do I convert the resulting void* into something I can process in D? //I have the following function from the GDAL C library. extern(C) CPLErr GDALReadBlock( GDALRasterBandH, int, int, void* ); So I have (GByte is defined in the GDAL library): void* buffer = malloc( GByte.sizeof * x_block_size * y_block_size ); I fill the buffer (and ignore any errors :o) GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, buffer ); Now, how can I access the data in buffer? Or you probably can do it like this: auto buffer = new GByte[xblock*yblock]; GDALReadBlock( AGDALRasterBandHInstance, xblock, yblock, (cast void*)buffer.ptr ); But in this case memory will be scan by GC. Which probably is not something what you want.
Re: alias this of non-public member
On Tue, 07 Apr 2015 16:40:29 + via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the alias this mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :) Works for me: struct M { void callMe() { writeln(Ring...); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); }
Re: alias this of non-public member
On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote: On Tue, 07 Apr 2015 16:40:29 + via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the alias this mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :) Works for me: struct M { void callMe() { writeln(Ring...); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); } module some; import std.stdio; Another way is use template mixin: private mixin template M() { int someVar = 7; public void callMe() { writeln(Call); } public void callMe2() { writeln(Call2); } } struct S { mixin M; } module main; import some; void main(string[] args) { S s; s.callMe(); s.callMe2(); }
Re: alias this of non-public member
On Tuesday, 7 April 2015 at 17:43:08 UTC, Daniel Kozak wrote: On Tuesday, 7 April 2015 at 17:21:09 UTC, Daniel Kozak wrote: On Tue, 07 Apr 2015 16:40:29 + via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi! Excuse me if this is obvious, but I can't recall coming across anything similar and a quick search returns nothing relevant: struct Foo { } struct FooWrapper { alias x_ this; private Foo* x_; // doesn't work, as x_ is private } Basically, I want x_ to never be visible, except through the alias this mechanism, at which point it should instead be seen as public. Assuming something like this is not already possible in a clean way, I would like to suggest a tiny(I think) addition to the language: struct FooWrapper { public alias x_ this; // overrides the visibility through the alias; private Foo* x_; } While I think this would be useful for the language, the reason I want such a wrapper, is because I want to give opIndex, toString, to a pointer, or, in fact just value semantics, while keeping the rest of the interface through the pointer. I thought about using a class instead of a struct pointer, but I am not sure about the memory layout for classes, nor about the efficiency of overriding Object's methods, so I didn't want to risk making it any less efficient. If someone could shed some light about D's class memory layout and general performance differences to a simple struct (or a C++ class for that matter), that would also be great. In general, more information about these sort of things would be great for us also-C++ programmers. :) Works for me: struct M { void callMe() { writeln(Ring...); } } struct S { alias m this; private M m; } void main(string[] args) { S s; s.callMe(); } module some; import std.stdio; Another way is use template mixin: private mixin template M() { int someVar = 7; public void callMe() { writeln(Call); } public void callMe2() { writeln(Call2); } } struct S { mixin M; } module main; import some; void main(string[] args) { S s; s.callMe(); s.callMe2(); } And maybe Proxy can be use for your use case: http://dlang.org/phobos/std_typecons.html#.Proxy
Re: Parameter is null by default. No value is given. Code says it is not null.
On Thursday, 9 April 2015 at 14:42:33 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:30:07 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote: By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 14.04. I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for me. WOW rdmd app.d(without params): Ok rdmd and dub works because they are use ldc, but do not me ask how. I always think that dub and rdmd should use dmd compiler until I tell them otherwise I try it with DMD64 D Compiler v2.066 and same problem occured. So probably some backend problem. You should create an issue on https://issues.dlang.org
Re: Parameter is null by default. No value is given. Code says it is not null.
On Thursday, 9 April 2015 at 14:30:07 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote: By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 14.04. I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for me. WOW rdmd app.d(without params): Ok rdmd and dub works because they are use ldc, but do not me ask how. I always think that dub and rdmd should use dmd compiler until I tell them otherwise
Re: Parameter is null by default. No value is given. Code says it is not null.
On Thursday, 9 April 2015 at 14:25:56 UTC, Daniel Kozak wrote: On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote: By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 14.04. I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for me. WOW rdmd app.d(without params): Name: A 1 Expires null: true Path equals null: true Domain null: true Name: A 2 Expires null: true Path equals null: true Domain null: true Name: A 3 Expires null: true Path equals null: true Domain null: true Name: A 4 Expires null: true Path equals null: true Domain null: true Name: A 5 Expires null: true Path equals null: true Domain null: true ~ Name: A 6 Expires null: true Path equals null: true Domain null: true Name: A 7 Expires null: true Path equals null: true Domain null: true Name: A 8 Expires null: true Path equals null: true Domain null: true Name: A 9 Expires null: true Path equals null: true Domain null: true ~ Name: A10 Expires null: true Path equals null: true Domain null: true Name: A11 Expires null: true Path equals null: true Domain null: true Name: A12 Expires null: true Path equals null: true Domain null: true Name: A13 Expires null: true Path equals null: true Domain null: true dmd -O: Name: A 1 Expires null: true Path equals null: false Domain null: false Name: A 2 Expires null: true Path equals null: false Domain null: false Name: A 3 Expires null: true Path equals null: false Domain null: false Name: A 4 Expires null: true Path equals null: false Domain null: false Name: A 5 Expires null: true Path equals null: false Domain null: false ~ Name: A 6 Expires null: true Path equals null: false Domain null: false Name: A 7 Expires null: true Path equals null: false Domain null: false Name: A 8 Expires null: true Path equals null: false Domain null: false Name: A 9 Expires null: true Path equals null: false Domain null: false ~ Name: A10 Expires null: true Path equals null: false Domain null: false Name: A11 Expires null: true Path equals null: false Domain null: false Name: A12 Expires null: true Path equals null: false Domain null: false Name: A13 Expires null: true Path equals null: false Domain null: false dmd -release: Name: A 1 Expires null: true Path equals null: true Domain null: true Name: A 2 Expires null: true Path equals null: false Domain null: false Name: A 3 Expires null: true Path equals null: false Domain null: false Name: A 4 Expires null: true Path equals null: false Domain null: false Name: A 5 Expires null: true Path equals null: false Domain null: false ~ Name: A 6 Expires null: true Path equals null: false Domain null: false Name: A 7 Expires null: true Path equals null: false Domain null: false Name: A 8 Expires null: true Path equals null: false Domain null: false Name: A 9 Expires null: true Path equals null: false Domain null: false ~ Name: A10 Expires null: true Path equals null: false Domain null: false Name: A11 Expires null: true Path equals null: false Domain null: false Name: A12 Expires null: true Path equals null: false Domain null: false Name: A13 Expires null: true Path equals null: false Domain null: false with ldc everything is ok
Re: Parameter is null by default. No value is given. Code says it is not null.
On Thursday, 9 April 2015 at 14:16:00 UTC, tcak wrote: By the way, I am using DMD64 D Compiler v2.067.0 on Ubuntu 14.04. I have Archlinux DMD64 D Compiler v2.067.0 and it works OK for me.
Re: Template type deduction and specialization
On Wednesday, 20 May 2015 at 09:24:28 UTC, Daniel Kozák wrote: On Wed, 20 May 2015 06:31:11 + Mike Parker via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I don't understand why this behaves as it does. Given the following two templates: ``` void printVal(T)(T t) { writeln(t); } void printVal(T : T*)(T* t) { writeln(*t); } ``` I find that I actually have to explicitly instantiate the template with a pointer type to get the specialization. ``` void main() { int x = 100; printVal(x); int* px = x; printVal(px);// prints the address printVal!(int*)(px) // prints 100 } ``` Intuitively, I would expect the specialization to be deduced without explicit instantiation. Assuming this isn't a bug (I've been unable to turn up anything in Bugzilla), could someone in the know explain the rationale behind this? Because it cannot deduce type T: try this: void printVal(T : T*)(T* t) { writeln(*t); } void main() { int x = 100; int* px = x; printVal(px); } It will print error. My advise is not to use T:T* or T:T[] it works only when explicitly instantiate. Is better use T:M*,M or T:M[], M because it works automaticly and you have both types available. import std.stdio; void printVal(T)(T t) { writeln(t); } void printVal(T:M*,M)(T t) { writeln(*t); } void main() { int x = 100; printVal(x); int* px = x; printVal(px);// prints the 100 } DOCS: http://dlang.org/template.html#function-templates says: Function template type parameters that are to be implicitly deduced may not have specializations:
Re: Template type deduction and specialization
On Wednesday, 20 May 2015 at 07:27:53 UTC, jklp wrote: --- import std.stdio; void printVal(T)(T t) { writeln(t); } void printVal(T: T)(T* t) { writeln(*t); } void main() { int x = 100; printVal(x); int* px = x; printVal(px); } --- here it's selected correctly without explicit instantiation. But honestly i don't know why since the asterisk is removed from the T it looks quite incorrect. No it is correct it is same as: void printVal(T: int)(T* t) { writeln(*t); }
Re: Template type deduction and specialization
On Wednesday, 20 May 2015 at 09:35:48 UTC, Jonathan M Davis wrote: Well, if printVal!(int*)(px); prints 100, then that's a bug. It should print the address. In fact, it should be _impossible_ for the second overload of printVal to ever be instantiated IMHO thats not true, it should print 100. This is what spec say. void printVal(T : T*)(T* t) { writeln(*t); } T is deduce to be int so we have void printVal(int* t) { writeln(*t); } which will print value not address
Re: Utf8 to Utf32 cast cost
On Mon, 08 Jun 2015 18:16:57 + Anonymouse via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On Monday, 8 June 2015 at 11:44:47 UTC, Daniel Kozák wrote: No difference even with GC.disable() results are same. Profile! Callgrind is your friend~ Yep, but I dont care, I am the one who makes transcode faster, so I am happy with results :P. P.S. I care and probably when I have some spare time I will improve to!dstring too
Re: Abstract sockets (linux)
On Thu, 25 Jun 2015 15:56:04 + freeman via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: I am having trouble using abstract sockets on Linux. Here is sample python code that works, which works: ptm_sockname = \0/var/run/ptmd.socket sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(ptm_sockname) sock.setblocking(1) sock.sendall('get-status detail') Similar code in D, which does not work: string socket_name = \0/var/run/ptmd.socket; auto address = new UnixAddress(socket_name); auto sock = new Socket(AddressFamily.UNIX, SocketType.STREAM); scope(exit) sock.close(); sock.blocking = true; sock.connect(address); sock.send(get-status detail); This is the equivalent with socat, which works: $ echo get-status detail | socat - ABSTRACT-CLIENT:/var/run/ptmd.socket My test D program exits on connect: std.socket.SocketOSException@runtime/phobos/std/socket.d(2674): Unable to connect socket: Connection refused Any pointers? instead of: string socket_name = \0/var/run/ptmd.socket; try: string socket_name = /var/run/ptmd.socket; works for me
Re: Qualified destructors / immutable objects
On Friday, 12 June 2015 at 15:36:22 UTC, anonymous wrote: no need for ~this() to modify immutable data: class C { int a; this(int a) { this.a = a; } } struct S { C elem = new C(42); } void main() { import std.stdio; immutable(S) s1; // Error: cannot modify immutable expression s1.elem.a // s1.elem.a = 43; writeln(s1.elem.a); S s2; s2.elem.a = 123; writeln(s1.elem.a); } Prints: 42 123 Is there an existing issue on issue.dlang.org? If not can you report it
Re: Weird result of getsockopt
On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote: Hello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t*)size); printf(%d\n, flag); } C version: #include sys/socket.h #include arpa/inet.h #include stdio.h int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t*)size); printf(%d\n, flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct? Cause your code is wrong: If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value. So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything
Re: Weird result of getsockopt
On Sunday, 24 May 2015 at 21:13:02 UTC, Daniel Kozak wrote: On Sunday, 24 May 2015 at 21:11:34 UTC, Daniel Kozak wrote: On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote: Hello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t*)size); printf(%d\n, flag); } C version: #include sys/socket.h #include arpa/inet.h #include stdio.h int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t*)size); printf(%d\n, flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct? Cause your code is wrong: If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value. So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything *call it again with right size and than it will probably work I mean call it with something big enought, size than should be modified to real size
Re: Weird result of getsockopt
On Sunday, 24 May 2015 at 21:11:34 UTC, Daniel Kozak wrote: On Sunday, 24 May 2015 at 16:51:44 UTC, CodeSun wrote: Hello guys, Today, I found a weird problem when I was learning to enable SO_KEEPALIVE for a specific socket. I use setsockopt to enable keepalive firstly, and then use getsockopt to show if it is enabled correctly. My code snippet is listed below: Dlang version: import core.sys.posix.sys.socket; import core.sys.posix.netinet.in_; import std.c.stdio; void main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t)flag.sizeof); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, cast(socklen_t*)size); printf(%d\n, flag); } C version: #include sys/socket.h #include arpa/inet.h #include stdio.h int main() { int sfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); int flag = 1; size_t size; setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t)sizeof(flag)); flag = 0; getsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, flag, (socklen_t*)size); printf(%d\n, flag); return 0; } Dlang version always prints 0, which means keepalive is not enabled, while C version can almost display 1 all the time as expected. So is there anything wrong inside the code? If not, whose behavior is correct? Cause your code is wrong: If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value. So because you have size set to 0 it will not work, you mast call it again and than it will probably work. In C this work because size is not initialize which mean it could be anything *call it again with right size and than it will probably work
Re: Template type deduction and specialization
On Thursday, 21 May 2015 at 13:12:36 UTC, Daniel Kozák wrote: On Thu, 21 May 2015 08:54:54 -0400 Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On 5/21/15 2:35 AM, Daniel Kozák via Digitalmars-d-learn wrote: On Wed, 20 May 2015 17:23:05 -0700 Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: On 05/20/2015 04:10 PM, Mike Parker wrote: On Wednesday, 20 May 2015 at 13:46:22 UTC, Daniel Kozák wrote: DOC say `may not have` not `must not have` ;-) OK, if that's the intent, it needs to be reworded. As it stands, it looks more like it's saying specialization is not permissible, rather than what might be possible. That's the only meaning that I get: The doc means must not. Yet, as you've shown, the behavior does not match the doc. Ali 1.) we could fix just doc - easiest, but inconsistent Before doing this, we have to understand what works and what doesn't. It's not clear to me. 2.) remove implicit deduction even for fun(T:char)(T c) and all other specialization - code breakage so imho not good I don't think this is possible, this would break lots of existing code. 3.) fix doc and allow even fun(T:T*)(T* p) - same as 2 I agree with this fix. I don't understand why specialization should disqualify IFTI. Can someone explain this rationale besides because the docs say so? But this will break more code than 2. So it is impossible to fix it. Not more, but it will be worst, because it could change behaviour of program without error message
Re: Static arrays inside struct and class - bug?
V Sat, 01 Aug 2015 19:21:36 + NX via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Saturday, 1 August 2015 at 18:50:09 UTC, Daniel Kozak wrote: No you don't. You still use static allocation for array Can clarify why does that happen and I still suspect it's a static allocation it would increase output exe if it was really that static..? No it would not increase output exe. Problem is with definition: type[size] val; // static declaration so compilere check max 16M. But you are right, in your case it could be improved and such declaration could work. because: S { byte[16*1024*1024*1024] arr; } void main() { auto s = new S(); if (s is null) { // error cannont allocate enought memory } } but: void main() { byte[16*1024*1024*1024] arr; // impossible to check if is allocated } Maybe you can open an enhancment on issues.dlang.org
Re: Static arrays inside struct and class - bug?
On Saturday, 1 August 2015 at 18:07:51 UTC, NX wrote: On Saturday, 1 August 2015 at 17:29:54 UTC, Adam D. Ruppe wrote: Sorry, I can't see _the_ point in that. I understand that could be a problem if it was a global array but this scenery is completely wrong in my view. I'm already going to dynamically allocate it No you don't. You still use static allocation for array
Re: Static arrays inside struct and class - bug?
V Sat, 01 Aug 2015 19:16:16 + NX via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Saturday, 1 August 2015 at 18:47:00 UTC, Daniel Kozak wrote: Still same problem, You can`t allocate more then 16M on stack. Use dynamic allocation I don't think new MyStruct allocates on stack, actually allocating ~16MB on stack will immediatelly crash the program which is not the case with NewExpression. My fault It is not on stack, but still it is a static allocation
Re: Static arrays inside struct and class - bug?
V Sat, 01 Aug 2015 18:07:50 + NX via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno: On Saturday, 1 August 2015 at 17:29:54 UTC, Adam D. Ruppe wrote: On Saturday, 1 August 2015 at 17:22:40 UTC, NX wrote: I wonder if the followings are compiler bugs: No, it is by design, the idea is to keep static arrays smallish so null references will be caught by the processor. (An overly large static array could allow indexing it through a null pointer to potentially reach another object.) The easiest workaround is to just dynamically allocate such huge arrays: byte[] arr = new byte[](1024*1024*16); ReadProcessMemory(Proc, 0xdeadbeef, arr.ptr, arr.length, null); The arr.ptr and arr.length are the key arguments there. Sorry, I can't see _the_ point in that. I understand that could be a problem if it was a global array but this scenery is completely wrong in my view. I'm already going to dynamically allocate it and my problem is actually a lot complex than what I showed there, I not even allowed to do this: struct stuff { byte[1024*1024*16] arr; // Error: index 16777216 overflow for static array } //... stuff* data = new stuff; ReadProcessMemory(Proc, (void*)0xA970F4, data, stuff.sizeof, null); Here (https://gist.github.com/NightmareX1337/6408287d7823c8a4ba20) is the real issue if anyone want to see the real-world problem with long lines of code Still same problem, You can`t allocate more then 16M on stack. Use dynamic allocation
Re: [dmd2.068] Bug or future?
On Friday, 7 August 2015 at 06:26:21 UTC, VlasovRoman wrote: I have some code: import std.stdio; auto dot(T, R)(T x, R y) { return x * y; } struct Vector(T) { alias selftype = Vector!T; int len = 5; pure: const @property{ static if( is( typeof( dot( selftype.init, selftype.init ) ) ) ){ auto len2() {return len * len;} } static if(is(typeof(T.init * T.init) == T)) { auto e() {return len;} } } } void main() { Vector!(float) vec; float x = vec.len2(); writeln(x); x = vec.e(); writeln(x); } I get error by compiler when i build this: main.d(30): Error: no property 'len2' for type 'Vector!float', did you mean 'len'? In dmd 2.067 is normaly. is it Bug or enhancements? Does not work in 2.067 for me. Btw. you do not need to do this: alias selftype = Vector!T; You can just use Vector, or: alias selftype = Vector; if you prefer selftype as a name.
Re: zlib performance
On Friday, 7 August 2015 at 09:12:32 UTC, yawniek wrote: On Friday, 7 August 2015 at 08:50:11 UTC, Daniel Kozák wrote: ldc[2] -O -release -boundscheck=off -singleobj app.d ldc 0.15.2 beta2 2.86s user 0.55s system 77% cpu 4.392 total v2.068-devel-8f81ffc 2.86s user 0.67s system 78% cpu 4.476 total v2.067 2.88s user 0.67s system 78% cpu 4.529 total i can now reproduce the results and indeed, its faster than zcat: on a c4.xlarge aws instance running archlinux and dmd v2.067 same file as above on my macbook. best run: 2.72s user 0.39s system 99% cpu 3.134 total worst run: 3.47s user 0.46s system 99% cpu 3.970 total zcat: best: 4.45s user 0.28s system 99% cpu 4.764 total worst: 4.99s user 0.57s system 99% cpu 5.568 total so i guess on os x there is still something to be optimized Can you try it without write operation (comment out all write)? And than try it without uncompression? // without compression: void main(string[] args) { auto f = File(args[1], r); foreach (buffer; f.byChunk(4096)) { write(cast(char[])buffer); } } // without write: void main(string[] args) { auto f = File(args[1], r); auto uncompressor = new UnCompress(HeaderFormat.gzip); foreach (buffer; f.byChunk(4096)) { auto uncompressed = cast(char[])(uncompressor.uncompress(buffer)); } uncompressor.flush; }
Re: What D really needs :D
On Saturday, 25 July 2015 at 18:30:16 UTC, Daniel Kozak wrote: https://github.com/avinassh/rockstar?utm_content=buffer64b3cutm_medium=socialutm_source=twitter.comutm_campaign=buffer Wrong forum, someone put learn on top. http://forum.dlang.org/post/ooybhdoonxoxkzdsz...@forum.dlang.org
What D really needs :D
https://github.com/avinassh/rockstar?utm_content=buffer64b3cutm_medium=socialutm_source=twitter.comutm_campaign=buffer
Re: static linking
On Sat, 25 Jul 2015 18:02:46 + Laeeth Isharc via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote: Hi. Is static linking with dmd still broken on linux? If so, can I link statically with gdc or ldc, and if so how? https://issues.dlang.org/show_bug.cgi?id=12268 I am trying to compile a D binary to run on AWS lambda. If I cannot link statically, which files should I include in the zip upload - libphobos2.so, libdruntime-linux64so.o ? Thanks. Laeeth. Do you mean dynamic linking? I do not have any problems with dmd. Can you please post more details what you are trying? *.so are dynamic, *.[l]a are static
Re: OSX Foundation framework D binding
V Wed, 11 Nov 2015 06:17:00 + Vadim Lopatin via Digitalmars-d-learnnapsáno: > Hello, > > I'm working on native Cocoa backend for DlangUI GUI library under > OSX. > Is there any ready to use bindings for easy accessing Cocoa API? > Probably, there is some HelloWorld program which creates window > and draws something? > > > Best regards, > Vadim I find only this one: http://code.dlang.org/packages/derelict-cocoa
Re: my first D program (and benchmark against perl)
On Wednesday, 11 November 2015 at 13:32:00 UTC, perlancar wrote: Here's my first non-hello-world D program, which is a direct translation from the Perl version. I was trying to get a feel about D's performance: ... While I am quite impressed with how easy I was able to write D, I am not so impressed with the performance. Using rdmd (build 20151103), the D program runs in 17.127s while the Perl version runs in 11.391s (so the D version is quite a bit *slower* than Perl's). While using gdc (Debian 4.9.2-10), I am able to run it in 3.988s (only about 3x faster than Perl's version). I understand that string processing (concatenation, allocation) is quite optimized in Perl, I was wondering if the D version could still be sped up significantly? Main problem is with allocations and with stripLeft, here is my version which is 10x faster than perls even with DMD. With LDC is 12x faster import std.stdio; import std.array : appender; import std.range; auto fmttable(T)(T table) { auto res = appender!(string)(); res.reserve(64); if (table.length == 0) return ""; // column widths auto widths = new int[](table[0].length); foreach (rownum, row; table) { foreach (colnum, cell; row) { if (cell.length > widths[colnum]) widths[colnum] = cast(int)cell.length; } } foreach (row; table) { res.put("|"); foreach (colnum, cell; row) { int l = widths[colnum] - cast(int)cell.length; res.put(cell); if (l) res.put(' '.repeat().take(l)); res.put("|"); } res.put("\n"); } return res.data; } void main() { auto table = [ ["row1.1", "row1.2 ", "row1.3"], ["row2.1", "row2.2", "row2.3"], ["row3.1", "row3.2", "row3.3 "], ["row4.1", "row4.2", "row4.3"], ["row5.1", "row5.2", "row5.3"], ]; write(fmttable(table)); for (int i=0; i < 100; ++i) { fmttable(table); } }
Re: my first D program (and benchmark against perl)
V Thu, 12 Nov 2015 09:12:32 + Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Wednesday, 11 November 2015 at 13:32:00 UTC, perlancar wrote: > > Here's my first non-hello-world D program, which is a direct > > translation from the Perl version. I was trying to get a feel > > about D's performance: > > > > ... > > > > While I am quite impressed with how easy I was able to write D, > > I am not so impressed with the performance. Using rdmd (build > > 20151103), the D program runs in 17.127s while the Perl version > > runs in 11.391s (so the D version is quite a bit *slower* than > > Perl's). While using gdc (Debian 4.9.2-10), I am able to run it > > in 3.988s (only about 3x faster than Perl's version). > > > > I understand that string processing (concatenation, allocation) > > is quite optimized in Perl, I was wondering if the D version > > could still be sped up significantly? > > Main problem is with allocations and with stripLeft, here is my > version which is 10x faster than perls even with DMD. With LDC is > 12x faster > > import std.stdio; > import std.array : appender; > import std.range; > > > auto fmttable(T)(T table) { > auto res = appender!(string)(); > res.reserve(64); > > if (table.length == 0) return ""; > > // column widths > auto widths = new int[](table[0].length); > > foreach (rownum, row; table) { > foreach (colnum, cell; row) { > if (cell.length > widths[colnum]) > widths[colnum] = cast(int)cell.length; > } > } > > foreach (row; table) { > res.put("|"); > foreach (colnum, cell; row) { > int l = widths[colnum] - cast(int)cell.length; > res.put(cell); > if (l) >res.put(' > '.repeat().take(l)); res.put("|"); > } > res.put("\n"); > } > > return res.data; > } > > void main() { > > auto table = [ > ["row1.1", "row1.2 ", "row1.3"], > ["row2.1", "row2.2", "row2.3"], > ["row3.1", "row3.2", "row3.3 "], > ["row4.1", "row4.2", "row4.3"], > ["row5.1", "row5.2", "row5.3"], > ]; > > write(fmttable(table)); > for (int i=0; i < 100; ++i) { > fmttable(table); > } > } > > or with ~ operator: import std.stdio; auto fmttable(string[][] table) { import std.array : appender, uninitializedArray; import std.range : take, repeat; import std.exception : assumeUnique; auto res = appender(uninitializedArray!(char[])(128)); res.clear(); if (table.length == 0) return ""; // column widths auto widths = new int[](table[0].length); foreach (rownum, row; table) { foreach (colnum, cell; row) { if (cell.length > widths[colnum]) widths[colnum] = cast(int)cell.length; } } foreach (row; table) { res ~= "|"; foreach (colnum, cell; row) { int l = widths[colnum] - cast(int)cell.length; res ~= cell; if (l) res ~= ' '.repeat().take(l); res ~= "|"; } res.put("\n"); } return res.data.assumeUnique(); } void main() { auto table = [ ["row1.1", "row1.2 ", "row1.3"], ["row2.1", "row2.2", "row2.3"], ["row3.1", "row3.2", "row3.3 "], ["row4.1", "row4.2", "row4.3"], ["row5.1", "row5.2", "row5.3"], ]; write(fmttable(table)); for (int i=0; i < 100; ++i) { fmttable(table); } }
Re: my first D program (and benchmark against perl)
V Thu, 12 Nov 2015 11:03:38 + Tobias Pankrath via Digitalmars-d-learnnapsáno: > > or with ~ operator: > > > > import std.stdio; > > > > [...] > > Did anyone check that the last loop isn't optimized out? Yes, it is not optimized out > Could also be improved further if you make the function take an > output range and reuse one appender for every call, but that might be > to far off the original perl solution. I agree, that would be to far off the original solution.
Re: my first D program (and benchmark against perl)
V Thu, 12 Nov 2015 12:13:10 + perlancar via Digitalmars-d-learnnapsáno: > On Wednesday, 11 November 2015 at 14:20:51 UTC, Rikki Cattermole > wrote: > > I turned it into mostly using large allocations, instead of > > small ones. > > Although I'd recommend using Appender instead of my custom > > functions for this. > > > > Oh and for me, I got it at 2 secs, 513 ms, 397 μs, and 5 > > hnsecs. Unoptimized, using dmd. > > When release mode is enabled on dmd: 1 sec, 550 ms, 838 μs, and > > 9 hnsecs. So significant improvement even with dmds awful > > optimizer. > > Hi Rikki, > > Thanks. With your version, I've managed to be ~4x faster: > > dmd : 0m1.588s > dmd (release): 0m1.010s > gdc : 0m2.093s > ldc : 0m1.594s > > Perl version : 0m11.391s > > So, I'm satisfied enough with the speed for now. Turns out dmd is > not always slower. It depends which flags do you use on ldc and gdc ldc (-singleobj -release -O3 -boundscheck=off) gdc (-O3 -finline -frelease -fno-bounds-check)
Re: my first D program (and benchmark against perl)
On Thursday, 12 November 2015 at 12:25:08 UTC, Daniel Kozak wrote: V Thu, 12 Nov 2015 12:13:10 + perlancar via Digitalmars-d-learnnapsáno: On Wednesday, 11 November 2015 at 14:20:51 UTC, Rikki Cattermole wrote: > I turned it into mostly using large allocations, instead of > small ones. > Although I'd recommend using Appender instead of my custom > functions for this. > > Oh and for me, I got it at 2 secs, 513 ms, 397 μs, and 5 > hnsecs. Unoptimized, using dmd. > When release mode is enabled on dmd: 1 sec, 550 ms, 838 μs, > and > 9 hnsecs. So significant improvement even with dmds awful > optimizer. Hi Rikki, Thanks. With your version, I've managed to be ~4x faster: dmd : 0m1.588s dmd (release): 0m1.010s gdc : 0m2.093s ldc : 0m1.594s Perl version : 0m11.391s So, I'm satisfied enough with the speed for now. Turns out dmd is not always slower. It depends which flags do you use on ldc and gdc ldc (-singleobj -release -O3 -boundscheck=off) gdc (-O3 -finline -frelease -fno-bounds-check) import std.stdio; auto fmttable(string[][] table) { import std.array : appender, uninitializedArray; import std.range : take, repeat; import std.exception : assumeUnique; if (table.length == 0) return ""; // column widths auto widths = new int[](table[0].length); size_t total = (table[0].length + 1) * table.length + table.length; foreach (rownum, row; table) { foreach (colnum, cell; row) { if (cell.length > widths[colnum]) widths[colnum] = cast(int)cell.length; } } foreach (colWidth; widths) { total += colWidth * table.length; } auto res = appender(uninitializedArray!(char[])(total)); res.clear(); foreach (row; table) { res ~= "|"; foreach (colnum, cell; row) { int l = widths[colnum] - cast(int)cell.length; res ~= cell; if (l) res ~= ' '.repeat().take(l); res ~= "|"; } res.put("\n"); } return res.data.assumeUnique(); } void main() { auto table = [ ["row1.1", "row1.2 ", "row1.3"], ["row2.1", "row2.2", "row2.3"], ["row3.1", "row3.2", "row3.3 "], ["row4.1", "row4.2", "row4.3"], ["row5.1", "row5.2", "row5.3"], ]; writeln(fmttable(table)); for (int i=0; i < 100; ++i) { fmttable(table); } } dmd -O -release -inline -boundscheck=off asciitable.d real0m1.463s user0m1.453s sys 0m0.003s ldc2 -singleobj -release -O3 -boundscheck=off asciitable.d real0m0.945s user0m0.940s sys 0m0.000s gdc -O3 -finline -frelease -fno-bounds-check -o asciitable asciitable.d real0m0.618s user0m0.613s sys 0m0.000s perl: real0m14.198s user0m14.170s sys 0m0.000s
Re: my first D program (and benchmark against perl)
On Thursday, 12 November 2015 at 12:49:55 UTC, Daniel Kozak wrote: On Thursday, 12 November 2015 at 12:25:08 UTC, Daniel Kozak wrote: ... auto res = appender(uninitializedArray!(char[])(total)); res.clear(); ... this is faster for DMD and ldc: auto res = appender!(string)(); res.reserve(total); but for gdc(fronend version 2.066) it makes it two times slower (same for dmd, ldc 2.066 and older)