Re: Inherit from class based on bool value
On 11/12/2018 11:10 PM, Jamie wrote: > I would like my class to inherit from one of two classes based on a > boolean value known at compile time. Something like this: > > void main() > { > Top!(OPTION.FALSE) top = new Top!(OPTION.FALSE); > } > > enum OPTION > { > FALSE = 0., > TRUE = 1. > } > > class One > {} > > class Two > {} > > class Top(OPTION option) : option ? One : Two > {} > > Is this possible? I can't get it to work in the way I'm showing above. > Cheers I got it working inside an eponymous template. D is pretty cool actually. :) enum OPTION { FALSE = 0., TRUE = 1. } class One {} class Two {} template Top(OPTION option) { static if (option == OPTION.TRUE) { alias Base = One; } else { alias Base = Two; } class Top : Base {} } void main() { auto a = new Top!(OPTION.FALSE); auto b = new Top!(OPTION.TRUE); } Ali
Inherit from class based on bool value
I would like my class to inherit from one of two classes based on a boolean value known at compile time. Something like this: void main() { Top!(OPTION.FALSE) top = new Top!(OPTION.FALSE); } enum OPTION { FALSE = 0., TRUE = 1. } class One {} class Two {} class Top(OPTION option) : option ? One : Two {} Is this possible? I can't get it to work in the way I'm showing above. Cheers
Re: Reading into the output of a long running shellExecute
On Saturday, 10 November 2018 at 15:54:07 UTC, JN wrote: On Saturday, 10 November 2018 at 15:05:38 UTC, helxi wrote: Hi. I have not done any multi-threaded programming before. What I basically want is to read into the output of a long shellExecute function each second. In details, I am calling shellExecute("pkexec dd if=/path/to/file of=/dev/sdx status=progress && sync"); It's a long running process and dd command prints how many bytes it has written in stdout continuously. I want to read and parse this output each second. How should I proceed? shellExecute won't work, because it waits for the process to end before moving on. I believe https://dlang.org/phobos/std_process.html#pipeProcess should do what you want. It returns a ProcessPipes object which has stdout, from which you should be able to read. Okay I looked it up but now I have another question 1 import std.process; 2 import std.stdio; 3 import std.range; 4 import std.string; 5 6 //pkexec dd if=/run/media/user1101/portable_drive/software/os/manjaro-kde-18.0-stable-x86_64.iso of=/dev/sdd bs=4M status=progress 2>&1 && sync 7 8 void main() { 9 auto pipe = pipeShell("bash", Redirect.stdout | Redirect.stderr | Redirect.stdin); 10 pipe.stdin.writeln( 11 "pkexec dd if=/run/media/user1101/portable_drive/software/os/manjaro-kde-18.0-stable-x86_64.iso of=/dev/sdd bs=4M status=progress 2>&1 && sync" 12 ); 13 pipe.stdin.flush(); 14 pipe.stdin.close(); 15 16 foreach (line; pipe.stdout.byLineCopy) 17 foreach (word; line.split) 18 writeln(word); 19 } What I want is to instantly print whatever dd prints to the stdout. (I made dd to redirect output to stdout with 2>&1 already). But the problem is, the pipe waits until dd finishes writing to stdout. How can I make the pipe not wait for dd to finish? }
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:29:24 UTC, helxi wrote: On Monday, 12 November 2018 at 16:25:13 UTC, Rene Zwanenburg wrote: Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers: import std.stdio; import std.concurrency; import core.thread; void worker(int firstNumber) { foreach (i; 0 .. 4) { Thread.sleep(500.msecs); writeln(firstNumber + i); } } void main() { foreach (i; 1 .. 3) { spawn(&worker, i * 10); } } Looks like worker needs an int and spawn(&worker, i * 10) seems to feed it's second arg to worker(?) That's right. spawn() is a function in the standard library that takes a function pointer, and all the arguments to pass to that function. It's a bit unusual in that regard: normally when using function pointers the arguments are provided by the code that receives the function pointer. Internally, spawn will call the function pointer just like I did in my example, but on another thread. Here's an example where a function pointer is passed around with the arguments provided by the callee: https://run.dlang.io/is/ArCN5t
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:29:24 UTC, helxi wrote: Looks like worker needs an int and spawn(&worker, i * 10) seems to feed it's second arg to worker(?) spawn is a template that takes a function pointer and a variable number of parameters. Both the pointer and the parameters are passed on to an internal _spawn function. https://github.com/dlang/phobos/blob/master/std/concurrency.d#L446 _spawn is has the same template parameters as spawn. It has an internal function that actually makes the call to the function pointer (fn): void exec() { thisInfo.ident = spawnTid; thisInfo.owner = ownerTid; fn(args); } https://github.com/dlang/phobos/blob/master/std/concurrency.d#L538 A few lines down from there, a pointer to exec is passed to either scheduler.spawn or the Thread constructor. When it's ultimately called, your function will be called in turn. At any rate, the actual call to the function pointer is fn(args).
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:29:24 UTC, helxi wrote: On Monday, 12 November 2018 at 16:25:13 UTC, Rene Zwanenburg wrote: Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers: import std.stdio; import std.concurrency; import core.thread; void worker(int firstNumber) { foreach (i; 0 .. 4) { Thread.sleep(500.msecs); writeln(firstNumber + i); } } void main() { foreach (i; 1 .. 3) { spawn(&worker, i * 10); } } Looks like worker needs an int and spawn(&worker, i * 10) seems to feed it's second arg to worker(?) Yes, seems so. Accordingly to https://dlang.org/library/std/concurrency/spawn.html However, there are more restrictions on input params in the notes section.
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:25:13 UTC, Rene Zwanenburg wrote: Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers: import std.stdio; import std.concurrency; import core.thread; void worker(int firstNumber) { foreach (i; 0 .. 4) { Thread.sleep(500.msecs); writeln(firstNumber + i); } } void main() { foreach (i; 1 .. 3) { spawn(&worker, i * 10); } } Looks like worker needs an int and spawn(&worker, i * 10) seems to feed it's second arg to worker(?)
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:08:28 UTC, helxi wrote: As far as I understand, calling a function pointer with an argument in D looks like: call(&fnptr, argTofn0, argTofn1, argTofn3); Idk where you got that syntax from, but there's no syntactic difference between calling normal functions and function pointers: https://run.dlang.io/is/I6u0rg
Re: How does calling function pointers work?
On Monday, 12 November 2018 at 16:08:28 UTC, helxi wrote: Line 12 was meant to print 1234. Line 13 was meant to print 1234 too, but for a different reason. Correction, it was meant to print 12304. My bad.
How does calling function pointers work?
As far as I understand, calling a function pointer with an argument in D looks like: call(&fnptr, argTofn0, argTofn1, argTofn3); This immediately struck me a very weak syntax to me so I decided to explore my concerns. I made a function pointer that takes an indefinite number of arguments. 1 import std.stdio; 2 3 int total(int[] numbers ...) { 4 int result; 5 for(ulong i = 0; i < numbers.length; result += numbers[i++]){} 6 return result; 7 } 8 9 10 void main() { 11 writeln(total(1000, 200, 30, 4)); // 1234 12 writeln(&total, 1000, 200, 30, 4); // 55CA386877AC1000200304 13 writeln((&total, 1000, 200, 30), 4); // error lmao 14 } How do you guys make writeln distinguish between an arg meant for writeln from an arg meant for &total? FYI Line 12 was meant to print 1234. Line 13 was meant to print 1234 too, but for a different reason.
Re: Why is stdio ... stdio?
On Monday, 12 November 2018 at 10:10:37 UTC, bauss wrote: I just want to say everyone who doesn't use the web-interface has to look at markdown anyway because people still write code in backticks etc. despite no support; even I do that. Me, too. It's easy and unobtrusive. As for actually rendering it, Validimir has commented on this in the past. From the D Blog: First, people using NNTP/email won’t see the rendered versions. Which isn’t a big deal by itself since it’s just text, but does create feature imparity. It *is* possible to write Markdown that looks fine when rendered but is unreadable in source form, especially with some common extensions such as GitHub Flavored Markdown. Second, unless we’re careful with this, people using NNTP/mailing lists might trigger Markdown formatting that could make their post unreadable. This could be avoided, though, by only rendering messages with Markdown if they originate from the web interface, which allows previewing posts." And that quote is followed by this commentary from me: "Even so, he’s hoping to add support for Markdown at some point in the future." https://dlang.org/blog/2016/06/10/core-team-update-vladimir-panteleev/
Re: Why is stdio ... stdio?
On Friday, 9 November 2018 at 06:42:37 UTC, Jonathan M Davis wrote: On Thursday, November 8, 2018 7:25:45 PM MST Neia Neutuladh via Digitalmars- d-learn wrote: It's not a forum. It's a newsgroup that happens to have a web interface. Newsgroups are text-only. So bbcode is out, html is out, but interpreting markdown might be reasonable. But nobody's done that work. Honestly, having markdown in messages being typical would be _really_ annoying for those of us not using the web interface, because we'd see all of those backticks and the like as backticks, not as syntax highlighting. It would be like seeing html, albeit far less intrusive. I for one would much rather that things just stay as pure text and that we not be adding any features to the web interface that encourages adding _any_ kind of markup to messages. The web interface makes it easier for folks who don't want to use a newsgroup or mailing list to interact with the newsgroup, but it's still a newsgroup, and _many_ of us use it as such. - Jonathan M Davis I just want to say everyone who doesn't use the web-interface has to look at markdown anyway because people still write code in backticks etc. despite no support; even I do that. Using markdown without support to me is not so much about whether it should be highlighted or not, but just that you can separate content from code in your message. Similar to how signatures are usually separated by 3 or more slashes and then a new line: Ex: --- Some signature