Re: How can I make this work?
On Sunday, 28 February 2021 at 13:15:47 UTC, Adam D. Ruppe wrote: On Sunday, 28 February 2021 at 07:05:27 UTC, Jack wrote: I'm using a windows callback function where the user-defined value is passed thought a LPARAM argument type. I'd like to pass my D array then access it from that callback function. How is the casting from LPARAM to my type array done in that case? The best way to do this is to put the array inside a struct and pass the address of the struct instead. This way both length and pointer are passed, and you have the option to add more things if you ended up needing it later, and there's fewer weird things to worry about. int[] arr = [1, 2, 3]; struct MyMessage { int[] arr; } // this far is easy enough MyMessage* messagePointer = new MyMessage(arr); // but we do need to tell the GC we intend to pass this to the outside world // failure to do this MIGHT lead to random crashes as the GC can't see it (it can't look inside the Windows message queue), assumes it is unused, and frees it out from under you. import core.memory; GC.addRoot(messagePointer); // when the GC has a root, it will consider that pointer live until further notice and not collect it nor its member variables. // so it is now cool to do this PostMessage(hwnd, MSG_WHATEVER, 0, cast(LPARAM) messagePointer); /* then on the other side */ switch(iMsg) { case MSG_WHATEVER: MyMessage* messagePointer = cast(MyMessage*) lParam; // need to tell the GC the pointer can be automatically managed normally again. failure to do this will lead to a memory leak import core.memory; GC.removeRoot(messagePointer); // now can use it foreach(item; messagePointer.arr) { // yada yada yada } } And it is the simplest thing, no missing length, no weird property casting. The GC handled with two simple add/remove calls. This is what I ended up using. using a single pointer such as MyMessage makes things much simpler. Thanks for the rememinder of GC.removeRoot() Everyone else in this theread, thank you guys. Always helpful
Re: D in AI Field
On Tuesday, 16 March 2021 at 03:33:55 UTC, Lasheen wrote: Hello, I'm a C programmer, and now i want to migrate to D. i work in AI field, so i have some questions about D language: 1- is D suitable for mission critical systems(such as avionics systems) ? if (true){ 2- what about safety, Security and reliability of D in Embedded systems ? 3- what about memory issues in D if i use it in AI ? 4- is there a compiler for AI ? }; Thank you. https://forum.dlang.org/thread/maqboaehjhmxvtssx...@forum.dlang.org
Re: Can't call splitter with range struct
On 3/16/21 1:58 AM, David Skluzacek wrote: > > Error: template std.algorithm.iteration.splitter cannot deduce function from argument types !()(GZippedFile, string), candidates are: > /usr/include/dlang/dmd/std/algorithm/iteration.d(4678): splitter(alias pred = "a == b", Range, Separator)(Range r, Separator s) >with pred = "a == b", > Range = GZippedFile, > Separator = string >must satisfy the following constraint: > is(typeof(binaryFun!pred(r.front, s)) : bool) That means that you should be able to call your predicate ("a == b") with GZippedFile.front and separator as arguments (they are dchar and string respectively) > (...) > > If I change the newline separator to a character literal, I get: > > (...) > /usr/include/dlang/dmd/std/algorithm/iteration.d(5055): splitter(alias pred = "a == b", Range, Separator)(Range r, Separator s) >with pred = "a == b", > Range = GZippedFile, > Separator = char >must satisfy the following constraint: > is(typeof(binaryFun!pred(r.front, s.front)) : bool) > > It seems like "\n" should pass the second constraint and '\n' should pass the first. Using a dchar or dstring makes no difference. Adding @property to front makes no difference. Is this a bug? > Also there are other constraints (see https://run.dlang.io/is/rcSJJf) like: /dlang/ldc-1.25.1/bin/../import/std/algorithm/iteration.d(5055): splitter(alias pred = "a == b", Range, Separator)(Range r, Separator s) with pred = "a == b", Range = GZippedFile, Separator = string must satisfy one of the following constraints: hasSlicing!Range isNarrowString!Range That means that you GZippedRange should provide opSlice operator and should be a narrow string (string of char or wchar)
Make foreach element optional
I find myself writing foreach (_; 0 .. n) doSomething(); // no using the variable `_` . What about relaxing the syntax to allow foreach (; 0 .. n) and/or foreach (0 .. n) ? Thereby making the `ForeachTypeList` of `AggregateForeach` in the grammar rule [1] optional. [1] https://dlang.org/spec/statement.html#foreach-statement
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 12:49:13 UTC, Per Nordlöw wrote: I find myself writing foreach (_; 0 .. n) doSomething(); // no using the variable `_` . What about relaxing the syntax to allow foreach (; 0 .. n) and/or foreach (0 .. n) ? Thereby making the `ForeachTypeList` of `AggregateForeach` in the grammar rule [1] optional. [1] https://dlang.org/spec/statement.html#foreach-statement foreach(0..n) could work. Why though.
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 13:52:29 UTC, Per Nordlöw wrote: On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote: foreach(0..n) could work. Why though. When performing a side-effect n times. Then why not just do: auto times(alias F, T)(T number) { return number.iota.each!(_ => F()); } void f() { writeln("hi"); } n.times!(f); ?
Re: How can I make this work?
On Sunday, 28 February 2021 at 13:15:47 UTC, Adam D. Ruppe wrote: And it is the simplest thing, no missing length, no weird property casting. The GC handled with two simple add/remove calls. Perfect example of teaching something. Thank you for this knowledge. Even though, this was not my problem, Its really helpful for me to my future project. :)
Re: How to open a compressed file in gz format ?
On Monday, 15 March 2021 at 01:36:08 UTC, sharkloc wrote: I want to read the content(file.gz) line by line,the following code is not friendly to large files of hundreds of Gb, and the memory overhead is also very large. You can use the internal zlib instead of a shell. This example is using stdin but you can it also replace with a file handle: import std.zlib; import std.stdio; import std.conv : to; import std.array : split; import std.algorithm.iteration : map; void main() { UnCompress decmp = new UnCompress; string buf; // read 4096 bytes of compressed stream at iteration foreach (chunk; stdin.byChunk(4096).map!(x => decmp.uncompress(x))) { // chunk has unknown length of decompressed data auto lines = to!string(chunk).split("\n"); foreach (i, line; lines[0 .. $]) { if (i == 0) { // if there is something in buffer // it belongs to previos line writeln(buf ~ line); // reset buffer buf.length = 0; } else if (i + 1 == lines.length) { // the last line is maybe incomplete, we never // directly output it buf = line; } else { writeln(line); } } } // rest if (buf.length) { write(buf); } }
Re: How can I make this work?
On Tuesday, 16 March 2021 at 16:02:14 UTC, Vinod K Chandran wrote: On Sunday, 28 February 2021 at 13:15:47 UTC, Adam D. Ruppe wrote: And it is the simplest thing, no missing length, no weird property casting. The GC handled with two simple add/remove calls. Perfect example of teaching something. Thank you for this knowledge. Even though, this was not my problem, Its really helpful for me to my future project. :) elegant approach
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote: foreach(0..n) could work. Why though. When performing a side-effect n times.
Re: Make foreach element optional
On 3/16/21 8:49 AM, Per Nordlöw wrote: I find myself writing foreach (_; 0 .. n) doSomething(); // no using the variable `_` . What about relaxing the syntax to allow foreach (; 0 .. n) and/or foreach (0 .. n) ? Thereby making the `ForeachTypeList` of `AggregateForeach` in the grammar rule [1] optional. [1] https://dlang.org/spec/statement.html#foreach-statement Meh, is this a common need though? The first form isn't terrible. In general, I'd say it would be nice to designate _ as an unused variable (i.e. not allowed to access it, and it doesn't trigger shadowing errors). It's like this in Swift for instance. -Steve
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 12:49:13 UTC, Per Nordlöw wrote: I find myself writing foreach (_; 0 .. n) doSomething(); // no using the variable `_` . What about relaxing the syntax to allow foreach (; 0 .. n) and/or foreach (0 .. n) ? Thereby making the `ForeachTypeList` of `AggregateForeach` in the grammar rule [1] optional. [1] https://dlang.org/spec/statement.html#foreach-statement The gain to altering the foreach statement is minimal since _ is a nice convention to use if you don't need the value of the counter. Something like this gives cleaner code: replicate(100) { // do stuff with side effects } I don't know if it would be an opportunity for a compiler optimization (probably not).
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 15:02:54 UTC, Steven Schveighoffer wrote: On 3/16/21 8:49 AM, Per Nordlöw wrote: I find myself writing foreach (_; 0 .. n) doSomething(); // no using the variable `_` . What about relaxing the syntax to allow foreach (; 0 .. n) and/or foreach (0 .. n) ? Thereby making the `ForeachTypeList` of `AggregateForeach` in the grammar rule [1] optional. [1] https://dlang.org/spec/statement.html#foreach-statement Meh, is this a common need though? The first form isn't terrible. In general, I'd say it would be nice to designate _ as an unused variable (i.e. not allowed to access it, and it doesn't trigger shadowing errors). It's like this in Swift for instance. -Steve the _ as unused variable would be useful when the parameter has out parameter but wouldn't to ignore it. C# does something like this currently. int foo(int x, out bool state) { } // only wants return value int y = foo(x, _);
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote: I see Do you get CLR_INVALID in return? As far as i know this is the value of CLR_INVALID - 4294967295. And these are the results i got from my function. Set Text color result - 0 Set Text color result - 36962150 Set Text color result - -1 Set Text color result - 0 Set Text color result - 36962150 Set Text color result - 0 Set Text color result - 36962150 The first two results got when the form shown. Rest are the results of a mouse hover.
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote: I see Do you get CLR_INVALID in return? From that results, second one contains my color value. Set Text color result - 0233FF66 RGB(102, 255, 51) is the color. 66 = 102 FF = 255 33 = 51
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote: I see Do you get CLR_INVALID in return? That results might be wrong. So i printed them in hex. These are the hex results. Set Text color result - Set Text color result - 0233FF66 Set Text color result - Set Text color result - Set Text color result - 0233FF66 Set Text color result - Set Text color result - 0233FF66 Look, the third one is CLR_INVALID.
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 19:15:02 UTC, Vinod K Chandran wrote: On Tuesday, 16 March 2021 at 18:35:00 UTC, Imperatorn wrote: I see Do you get CLR_INVALID in return? That results might be wrong. So i printed them in hex. These are the hex results. Set Text color result - Set Text color result - 0233FF66 Set Text color result - Set Text color result - Set Text color result - 0233FF66 Set Text color result - Set Text color result - 0233FF66 Look, the third one is CLR_INVALID. Some info here https://stackoverflow.com/questions/1525669/set-static-text-color-win32/1526240
Re: How to open a compressed file in gz format ?
On 3/14/21 9:36 PM, sharkloc wrote: I want to read the content(file.gz) line by line,the following code is not friendly to large files of hundreds of Gb, and the memory overhead is also very large. import std.stdio; import std.process; import std.string; void main(string[] args){ string fileName = args[1]; string command = "gzip -dc " ~ fileName ; auto dmd = executeShell(command); if(dmd.status != 0){ writeln("Compilation failed:\n", dmd.output); } else{ auto all=chomp(dmd.output).split("\n"); writeln(typeid(all)); for(int i=0; i It's not super-user-friendly, but iopipe excels at this kind of stuff (untested): // dub dependencies: [iopipe, io] import iopipe.bufpipe; import iopipe.textpipe; import iopipe.zip; import iopipe.refc; import std.io; import std.stdio; void main(string[] args) { string fileName = args[1]; auto lineRange = File(fileName) // open file .refCounted // make it copyable .bufd // buffer it .unzip // unzip it .assumeText // assume the binary data is utf8 text .byLineRange!true; // true = discard newlines foreach(line; lineRange) writeln(line); } -Steve
Re: Can't call splitter with range struct
On Tuesday, 16 March 2021 at 07:43:18 UTC, drug wrote: That means that you GZippedRange should provide opSlice operator and should be a narrow string (string of char or wchar) Yes, I should have looked more carefully at the doc, I was assuming splitter would accept a simple input range, but it doesn't. I really didn't want to provide opSlice because then if it were called with an index higher than the length of the buffer I'd have to read more data and allocate memory to hold it. I'm not actually trying to do this any more though. Thanks.
rdmd and D equivalent for PYTHONPATH?
Hi D I've writing little test scripts using rdmd to understand what various functions are really doing (ex: .take(5)). I'm up to the point where I need to write sample code to understand mir-algorithm a little better, but of course the library is not installed on my system. So two related questions: 1) After a "git clone" and "dub build" I'm left with redistributables that need to be put ... somewhere. But where? I tried this: cp -r -p source/mir /usr/local/include cp -p libmir-algorithm.a /usr/local/lib but no joy. So where can I install libs so that rdmd will find them? 2) On a related topic, is there an environment variable I can set (similar to PYTHONPATH, MATLABPATH, IDL_PATH, etc.) or a config file I can alter that will add to the module include path? I tried: #!/usr/bin/env DFLAGS="-I/usr/local/include" rdmd at the top of the file, but that just hung rdmd and nothing ran. Note: I'm aware of dub. This isn't a question about dub. I'm making scripts for local use, not redistributable binaries, so I would like to "install" mir-algorithm and similar libraries for my rdmd scripts to use. Thanks for the help,
Re: rdmd and D equivalent for PYTHONPATH?
On Wednesday, 17 March 2021 at 03:43:22 UTC, Chris Piker wrote: Note: I'm aware of dub. This isn't a question about dub. I'm making scripts for local use, not redistributable binaries, so I would like to "install" mir-algorithm and similar libraries for my rdmd scripts to use. Sorry to reply to myself, but I found something that will run the scripts, though it still doesn't make use of local libraries. #!/usr/bin/env dub /+ dub.sdl: dependency "mir-algorithm" version="~>1.0" +/ import mir.ndslice ... Since dub seems to work a bit better as the "interpreter" then rdmd (assuming you're connected to the Internet) why is this not promoted here: https://tour.dlang.org/tour/en/welcome/run-d-program-locally instead of rdmd? Thanks,
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 18:27:54 UTC, Vinod K Chandran wrote: On Tuesday, 16 March 2021 at 17:45:09 UTC, Imperatorn wrote: Omg the pain. Are you forced to use raw win api for this? Not at all. It's my hobby project. I choose raw win api. It's a fun. I see Do you get CLR_INVALID in return?
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 17:26:01 UTC, Vinod K Chandran wrote: Hi all, I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code. ``` uint setBtnBackColor( LPNMCUSTOMDRAW lp) { SetTextColor(lp.hdc, RGB(102, 255, 51) )// Not working if lp.uItemState & CDIS_SELECTED { //--- btn clicked // Change back color using SelectObject & FillRect // Its Working. No probs. } elseif lp.uItemState & CDIS_HOT { //Mouse over // Change back color using SelectObject & FillRect // Its Working. No probs. } else { // -Default state of button // Change back color using SelectObject & FillRect // Its Working. No probs. } return CDRF_SKIPDEFAULT } ``` What is wrong in my approach ? I'm afraid you have to do everything yourself, including draw the text and call SetTextColor() on the associated HDC (that you have used in the DrawText() for exe)
Re: Make foreach element optional
On Tuesday, 16 March 2021 at 16:29:45 UTC, Imperatorn wrote: On Tuesday, 16 March 2021 at 13:52:29 UTC, Per Nordlöw wrote: On Tuesday, 16 March 2021 at 13:31:34 UTC, Imperatorn wrote: foreach(0..n) could work. Why though. When performing a side-effect n times. Then why not just do: auto times(alias F, T)(T number) { return number.iota.each!(_ => F()); } void f() { writeln("hi"); } n.times!(f); ? To my knowledge, there's nothing like this in the standard library or the language. I used something similar but eventually decided it was simpler to use the original foreach. It'd honestly have to be a language change to be useful. (Given the benefit, I doubt this would happen.)
How to change button text color in NM_CUSTOMDRAW (Win32 API question)
Hi all, I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code. ``` uint setBtnBackColor( LPNMCUSTOMDRAW lp) { SetTextColor(lp.hdc, RGB(102, 255, 51) )// Not working if lp.uItemState & CDIS_SELECTED { //--- btn clicked // Change back color using SelectObject & FillRect // Its Working. No probs. } elseif lp.uItemState & CDIS_HOT { //Mouse over // Change back color using SelectObject & FillRect // Its Working. No probs. } else { // -Default state of button // Change back color using SelectObject & FillRect // Its Working. No probs. } return CDRF_SKIPDEFAULT } ``` What is wrong in my approach ?
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 17:26:01 UTC, Vinod K Chandran wrote: Hi all, I am creating a Button class with Win32 API functions. So far so good. I am using NM_CUSTOMDRAW message to change the back color of my buttons. It's really easy to change the back color in this way. But I can't change the text color of my button. This is my pseudo code. ``` uint setBtnBackColor( LPNMCUSTOMDRAW lp) { [...] Omg the pain. Are you forced to use raw win api for this?
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 17:45:09 UTC, Imperatorn wrote: Omg the pain. Are you forced to use raw win api for this? Not at all. It's my hobby project. I choose raw win api. It's a fun.
Re: Why are enums with base type string not considered strings?
On Sunday, 14 March 2021 at 16:30:47 UTC, Bastiaan Veelo wrote: On Sunday, 14 March 2021 at 16:09:39 UTC, Imperatorn wrote: On Sunday, 14 March 2021 at 10:42:17 UTC, wolframw wrote: [...] May be a regression? https://issues.dlang.org/show_bug.cgi?id=16573 Indeed: https://run.dlang.io/is/liSDBZ It regressed in 2.079.1. Seems to be worth an issue report. —Bastiaan. Thanks for the advice. I've since had a deeper look into this located the PR that changed this behavior [1]. It seems this change was very much deliberate. In the PR, Jonathan also makes some points that are very hard to disagree with. So, perhaps the better solution would be to make isBoolean and isSomeChar (and perhaps other functions that I didn't think of) return false for enums? As a side note, isSomeChar returning true for enums is also what causes the behavior demonstrated in Issue 21639 [2]. [1] https://github.com/dlang/phobos/pull/5291 [2] https://issues.dlang.org/show_bug.cgi?id=21639
Re: How to change button text color in NM_CUSTOMDRAW (Win32 API question)
On Tuesday, 16 March 2021 at 19:42:26 UTC, Imperatorn wrote: At last, i found the answer myself. There is a item called dwDrawStage in NMCUSTOMDRAW structure. If value of dwDrawStage is equal to CDDS_PREERASE, call SetBkMode with transparent and call SetTextColor. Then draw text with DrawText function. And finally, return CDRF_NOTIFYPOSTPAINT. In short, do the color changing process in pre-erase stage and return CDRF_NOTIFYPOSTPAINT constant. As per MSDN, What this constant means is, The control will notify the parent after painting an item. This occurs when the dwDrawStage of the NMCUSTOMDRAW structure equals CDDS_PREPAINT.
Re: How to delete dynamic array ?
On Monday, 30 December 2013 at 08:13:30 UTC, bearophile wrote: How to free memory to the system immediately? What is your use case? The use case is: we want deterministic memory management, (and the application data is too big to fit into memory at once, so have to be processed batch by batch). suppose: double[] data; // D type: dynamic array As of 2021 what's the correct way to allocate and deallocate (free memory to the system immediately) D's dynamic array? If we use core.stdc.stdlib.malloc and free, e.g. ``` data = core.stdc.stdlib.malloc(n * double.sizeof); // processing ... free(data.ptr); ``` -- will data.length be set correctly (since it's a D type) with malloc? -- or we still need to manually set data.length = n? (in this case, will it become GC managed, which we want to avoid in this app)? Thanks.
Re: How to delete dynamic array ?
On Tue, Mar 16, 2021 at 11:28:00PM +, mw via Digitalmars-d-learn wrote: [...] > suppose: > > double[] data; // D type: dynamic array > > As of 2021 what's the correct way to allocate and deallocate (free > memory to the system immediately) D's dynamic array? [...] Note that T[] is just a slice, not the dynamic array itself. The dynamic array is allocated and managed by the GC when you append stuff to it, or when you create a new array with `new` or an array literal. None of the latter, however, precludes you from using T[] for memory that you manage yourself. For example, you could do this: double[] data; data = cast(double[]) malloc(n * double.sizeof)[0 .. n]; Now you have a slice to memory you allocated yourself, and you have to manage its lifetime manually. When you're done with it: free(data.ptr); data = []; // null out dangling pointer, just in case The GC does not get involved unless you actually allocate from it. As long as .ptr does not point to GC-managed memory, the GC will not care about it. (Be aware, though, that the ~ and ~= operators may allocate from the GC, so you will have to refrain from using them. @nogc may help in this regard.) T -- Computers shouldn't beep through the keyhole.