Re: Dynamic array ot not
On 1/15/22 20:09, forkit wrote: > so at this link: https://dlang.org/spec/arrays.html > > it indicates an array of type[] is of type 'Dynamic array'. I have a problem with calling type[] a dynamic array because it is a slice, which may be providing access to the elements of a dynamic array. One complexity in D's slices is that they will start providing access to a dynamic array upon appending or concatenation. Here is the proof: void main() { int[2] data; int[] slice = data[]; } 'slice' has *nothing* to do with any dynamic array. Period! It will provide access to one with the following operation: slice ~= 42; Again, 'slice' is not a dynamic array; it provides access to the elements of one. (That dynamic array is owned by the D runtime (more precisely, the GC).) Despite that reality, people want dynamic arrays in the language and call slices dynamic arrays. Ok, I feel better now. :) > with that in mind, I ask, is this below a 'Dynamic array'. Nothing different from what I wrote above. > If not, why not? > > > int[][] mArr2 = array(iota(1, 9).chunks(2).map!array.array); mArr2 is a slice of slices. We can see from the initialization that there are many dynamic arrays behind the scenes. The outermost array() call is unnecessary because array() of a slice will produce another slice with the same type (perhaps e.g. a 'const' might be dropped) and elements. This is the same: int[][] mArr2 = iota(1, 9).chunks(2).map!array.array ; Ali
Dynamic array ot not
so at this link: https://dlang.org/spec/arrays.html it indicates an array of type[] is of type 'Dynamic array'. with that in mind, I ask, is this below a 'Dynamic array'. If not, why not? int[][] mArr2 = array(iota(1, 9).chunks(2).map!array.array);
Re: Starting and managing threads
On 1/12/22 00:50, Bagomot wrote: > If I change the run method of the Guard class so that it starts a new > thread, the program just does nothing: > ```d > public void run() { > spawn((shared EventLoop eventLoop) { > while ((cast() eventLoop).loop()) { > continue; The program does nothing probably because of that continue. (?) > } > }, cast(shared) this.eventLoop); > } So, the event loop is in a separate thread. What should happen when the events trigger? Do you want this thread to handle the events or should this thread send a message to the main thread (or perhaps a separate thread that responds to these events). Ali
Improve a simple event handler
I am writing a simple event handler object for observer pattern. https://gist.github.com/run-dlang/d58d084752a1f65148b33c796535a4e2 (note: the final implementation will use an array of listeners, but I want to keep it simple for now and have only one handler per event). Is there some way I could improve this with some D features? My main gripes with it are: 1) generic typing. I wish event handler methods such as onResize and onWindowTitle could take concrete types like WindowResize and WindowTitle rather than the generic Event struct. 2) same when emitting the event. It feels a bit verbose. I don't want to use classes or polymorphism. I was thinking perhaps std.variant could be useful here, although I am not exactly sure how it would work in practice.
Re: Starting and managing threads
On Saturday, 15 January 2022 at 19:07:20 UTC, frame wrote: On Wednesday, 12 January 2022 at 08:50:09 UTC, Bagomot wrote: Why? What am I doing wrong? I guess your main() exits and just ends all threads? No, the program continues to run. And I tested it with while in main.
Re: Starting and managing threads
On Wednesday, 12 January 2022 at 08:50:09 UTC, Bagomot wrote: Why? What am I doing wrong? I guess your main() exits and just ends all threads?
Re: Starting and managing threads
On Wednesday, 12 January 2022 at 08:50:09 UTC, Bagomot wrote: Good day! I keep giving rise to problems. Above, Tejas helped me a lot, but still doesn't work. I gave up using the fswatch library, thinking that the problem was in it. Now trying to do it using libasync. Here is the code that runs on the main thread, it blocks further actions on that thread. ```d #!/usr/bin/env dub /+ dub.sdl: dependency "libasync" version="~>0.8.6" +/ import std.stdio; import std.file; import std.algorithm; import core.thread; import std.concurrency; import libasync; import libasync.watcher; import libasync.threads; void main() { string testDir = "temp"; if (!testDir.exists) mkdir(testDir); Guard guard = Guard.getInstance; guard.addWatchedDir(testDir, false); guard.run; writeln("Some kind of action..."); } class Guard { private { __gshared Guard instance; static EventLoop eventLoop; WatchedDir[] watchedDirs; } protected this() { this.eventLoop = getThreadEventLoop(); } shared static ~this() { destroyAsyncThreads(); } public static Guard getInstance() { if (!instance) { synchronized (Guard.classinfo) { if (!instance) instance = new Guard; } } return instance; } public void run() { while (eventLoop.loop()) { continue; } } public void addWatchedDir(string dir, bool recursive = true, string[] exclude = [ ]) { if (this.watchedDirs.canFind!(a => a.dir == dir)) return; this.watchedDirs ~= new WatchedDir(dir, recursive, exclude); } class WatchedDir { private { string dir; bool recursive; string[] exclude; AsyncDirectoryWatcher watcher; DWChangeInfo[8] changeBuf; } this(string dir, bool recursive, string[] exclude) { this.dir = dir; this.recursive = recursive; this.exclude = exclude; this.watcher = new AsyncDirectoryWatcher(eventLoop); this.watcher.run({ DWChangeInfo[] changes = changeBuf[]; uint cnt; do { cnt = this.watcher.readChanges(changes); foreach (i; 0 .. cnt) { writeln("Main Callback got directory event: ", changes[i]); } } while (cnt > 0); }); this.watcher.watchDir(this.dir, DWFileEvent.ALL, this.recursive); } } } ``` If I change the run method of the Guard class so that it starts a new thread, the program just does nothing: ```d public void run() { spawn((shared EventLoop eventLoop) { while ((cast() eventLoop).loop()) { continue; } }, cast(shared) this.eventLoop); } ``` Why? What am I doing wrong? Actual!