Re: What does scope do as storage class?
On Saturday, 23 December 2017 at 21:20:13 UTC, Jonathan M Davis wrote: On Saturday, December 23, 2017 21:05:25 Marc via Digitalmars-d-learn wrote: for example: scope struct S { int x; } What does scope do here? Absolutely nothing. https://stackoverflow.com/questions/47240714/d-pure-classes-and-structs - Jonathan M Davis There's one special case, though, with scope in classes - scope will force user to allocate class as a scope: ``` scope class X { int x; } void main() { auto instance = new X; // ERROR must be scope instance = new X; } ```
Re: High-level wrapper for readline package
On Thursday, 7 September 2017 at 14:00:36 UTC, Nordlöw wrote: On Thursday, 7 September 2017 at 13:44:52 UTC, Adam D. Ruppe wrote: [...] There's always room for usability improvements when wrapping C APIs... [...] Minor point: you should add_history only if `lineStringz && lineStringz[0] != '\0'`
Re: How do I create a fileWatcher with an onFileChange event using spawn?
On Monday, 28 August 2017 at 06:27:20 UTC, Jacob Carlborg wrote: On 2017-08-25 23:25, Enjoys Math wrote: Something like this: module file_watcher; import std.concurrency; import std.file; import std.signals; import std.datetime; void fileWatcher(Tid tid, string filename, int loopSleep) { auto modified0 = timeLastModified(filename); while (true) { modified = timeLastModified(filename); if (modified > modified0) { modified0 = modified; //if (onFileChange !is null) // onFileChange(receiver); } sleep(dur!"msecs"(loopSleep)); } } But I'm not sure how to send the onFiledChange event. A delegate perhaps? Or you can look at any of the existing event driven libraries that do this: http://code.dlang.org/packages/vibe-core http://code.dlang.org/packages/libasync In addition, to avoid polling, it's possible to register yourself to the operating system so it will tell you when a modification on the given file has happened: https://msdn.microsoft.com/en-us/library/aa364417%28VS.85%29.aspx?f=255=-2147217396 http://man7.org/linux/man-pages/man7/inotify.7.html
Re: libc dependency
On Tuesday, 20 June 2017 at 11:18:17 UTC, Nemanja Boric wrote: Phobos (_not_ druntime) uses C _standard library_ abstractions for many things I just took a quick look and it seems that I'm remembering this wrong.
Re: libc dependency
On Tuesday, 20 June 2017 at 11:08:15 UTC, Moritz Maxeiner wrote: On Tuesday, 20 June 2017 at 11:00:00 UTC, Nemanja Boric wrote: On Monday, 19 June 2017 at 21:45:56 UTC, Moritz Maxeiner wrote: On Monday, 19 June 2017 at 21:35:56 UTC, Steven Schveighoffer wrote: IIRC, Tango did not depend on libc at all. It only used system calls. So it certainly is possible. How did they invoke those system calls? They are usually access via libc on POSIX systems, so you don't have to implement accessing e.g. vdso on Linux yourself. Tango just didn't use C abstractions (`FILE*`, say), but rather it would call functions such `read(2)`, `write(2)`, `seek(2)`, etc. and implement buffering/seeking/etc manually. Only the second one of those are the system calls I was talking about :p Yeah, I saw the Steve's answer late, but there's no editing posts here :(. :-) So, the library just declared `extern (C) read (...);`/`extern (C) write`.. and expected for the right library (which doesn't have to be libc, just the one that exposes these syscall wrappers) linked. If you have a library that exposes to you via C API those functions that you need of libc, then that library *is* your (albeit partial) libc (since a libc is defined as any library that exposes such functions). In any case, that's the same way that druntime depends on libc AFAIK. Yeah, that stands, but I thought the point of this topic was that (IIRC, not a phobos experienced user) Phobos (_not_ druntime) uses C _standard library_ abstractions for many things, and that's why the OP said we can't beat C in speed. I don't think it's reasonable thing to think that writing syscall wrappers would help D gain to much speed :-).
Re: libc dependency
On Monday, 19 June 2017 at 21:45:56 UTC, Moritz Maxeiner wrote: On Monday, 19 June 2017 at 21:35:56 UTC, Steven Schveighoffer wrote: IIRC, Tango did not depend on libc at all. It only used system calls. So it certainly is possible. How did they invoke those system calls? They are usually access via libc on POSIX systems, so you don't have to implement accessing e.g. vdso on Linux yourself. Tango just didn't use C abstractions (`FILE*`, say), but rather it would call functions such `read(2)`, `write(2)`, `seek(2)`, etc. and implement buffering/seeking/etc manually. So, the library just declared `extern (C) read (...);`/`extern (C) write`.. and expected for the right library (which doesn't have to be libc, just the one that exposes these syscall wrappers) linked.
Re: Array start index
On Monday, 6 February 2017 at 18:55:19 UTC, pineapple wrote: One reason for zero-based indexes that isn't "it's what we're all used to" is that if you used one-based indexes, you would be able to represent one fewer index than zero-based, since one of the representable values - zero - could no longer be used to represent any index. Also, it's what we're all used to, and it makes perfect sense to a lot of us, and the only times in recent memory I've ever made off-by-one errors were when I was trying to use Lua and its one-based indexing. Related: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
Re: Making .exe
On Thursday, 19 January 2017 at 18:58:31 UTC, Dlearner wrote: Hey! I wrote a little program that has an image bounce around and change colours, like the old DVD player screensavers. How can I build this as a little .exe file that I can send to someone? In the dub documentation there is something like `dub --build=`, but I'm not entirely sure what this does. Thanks! Hello! The binary should be in the working directory: ``` ➜ dub init Package recipe format (sdl/json) [json]: Name [test-dub]: Description [A minimal D application.]: Author name [Nemanja]: License [proprietary]: Copyright string [Copyright © 2017, Nemanja]: Add dependency (leave empty to skip) []: Successfully created an empty project in '/home/burgos/work/test-dub'. Package sucessfully created in . ➜ echo 'import std.stdio;\nvoid main() { writeln("Hello!"); }' > source/app.d ➜ dub --build=release Performing "release" build using dmd for x86_64. test-dub ~master: building configuration "application"... Linking... Running ./test-dub Hello! ➜ ./test-dub Hello! ➜ ll total 820K -rw-rw-r-- 1 burgos burgos 168 Jan 19 21:53 dub.json drwxrwxr-x 2 burgos burgos 4,0K Jan 19 21:52 source -rwxrwxr-x 2 burgos burgos 811K Jan 19 21:53 test-dub <- the executable ``` You can ommit --build=release - this makes a "release mode" binary (without assertions and bounds check), which is a faster & smaller version of the binary, but with less sanity checks.
Re: What do you use to generate documentation?
On Thursday, 19 January 2017 at 16:47:07 UTC, Basile B. wrote: On Wednesday, 13 March 2013 at 09:35:18 UTC, Andrea Fontana wrote: [...] I use harbored-mod (https://github.com/kiith-sa/hmod-dub). What I like about it: 1/ I'm not good with web things. The default styling is Okay for me so I don't have to lost time at writing some templates or others css files. 2/ I can call it in a single step from my IDE. For example after installing a static library, if i don't understand the API: - a double click to generate, using a custom tool, the doc using an IDE environment var that symbolize the sources files of the lib. - a double click to open the newly generated doc in the web-browser. It's really a no-brainer. Unfortunately It's not maintained anymore, but I'll go back to harbored the day the fork won't compile anymore. I've submitted few PRs but they never got merged. We're maintaining, unofficially, fork that does compile and it's available at: https://github.com/sociomantic-tsunami/harbored-mod
Re: Explain the Modules to me but better...
On Thursday, 29 December 2016 at 19:00:25 UTC, Nemanja Boric wrote: On Thursday, 29 December 2016 at 18:20:22 UTC, Modules Confuse Me wrote: [...] If you have following: [...] This all is valid, of course, only if I guessed right your problem :-)
Re: Explain the Modules to me but better...
On Thursday, 29 December 2016 at 18:20:22 UTC, Modules Confuse Me wrote: I'm trying to get going with D, but I keep getting hung up on modules. I personally like having many smaller files. Generally classes and interfaces all go into their own file. Even if the overall file ends up being smaller than 10 lines of real code. After reading: https://dlang.org/spec/module.html I seem to have at least enough information to get things working, but things break down after I start trying to package everything up into packages. Because it's a pain in the butt to have to include my interface files in separate imports than the implemented classes. So, I see D has packages, but whenever I use them I start getting errors like: Error: linker exited with status 180314872 I'm really getting hung up on a simple thing, such as how to structure my program in the 'D' way. So correct me if I am wrong. In my packages, I should be using 'public' imports correct? So that the imports get forwarded. I'm looking through phobos, to try to get a good idea of how others do it, and I don't see myself doing things too differently... If you have following: ``` main.d import a.mod; import a.mod2; void main() { foo(); bar(); } ``` ``` a/mod.d module a.mod; void foo() {} ``` ``` a/mod2.d module a.mod2; void bar() {} ``` And you try to compile this with ``` dmd main.d ``` you will get the following error (depends on your system) ``` main.o: In function `_Dmain': main.d:(.text._Dmain+0x5): undefined reference to `_D1a3mod3fooFZv' main.d:(.text._Dmain+0xa): undefined reference to `_D1a4mod23barFZv' collect2: error: ld returned 1 exit status Error: linker exited with status 1 ``` The reason for this is that dmd doesn't build any module except the ones in the command line (main.d in your case). So, when it tries to link, it will not find object files for foo and bar that are contained in the different module. Solution could be to pass all files to dmd, so it will build them all: ``` dmd main.d a/mod.d a/mod2.d ``` or to use `rdmd` which will figure out which files needs building, so it will do that for you manually: ``` rdmd main.d ```
Re: Best way to clear dynamic array for reuse
On Wednesday, 28 December 2016 at 12:30:33 UTC, Michael Rynn wrote: It takes a bit of work to get around the mutable buffers problem in D language arrays. I found it made a performance difference in xml parsing. https://github.com/betrixed/dlang-xml/blob/master/xml/util/buffer.d /** Buffer(T) - Versatile appendable D array for buffer reuse, append, reset with preserved capacity. Has additional template specializations for char, wchar, dchar, which always has an extra space for appending a terminating null character. Magic property is no memory reallocation or capacity loss if length set to zero. Resulting speed ups from reduced memory manager work. Perhaps reduction in hardware cache misses. Almost a drop in replacement for T[]. Some features adopted from std.array and std.range */ See also: https://github.com/sociomantic-tsunami/ocean/blob/v2.x.x/src/ocean/core/Buffer.d
Re: understanding std.algorithm.mutation.fill behaivor.
On Wednesday, 28 December 2016 at 05:09:34 UTC, LeqxLeqx wrote: Perhaps this is a stupid question, and I apologize if it is, but why doesn't this compile: import std.algorithm; import std.stdio; void main() { char[] array = [1, 2, 3, 4]; char value = 2; fill(array, value); } if this does: import std.algorithm; import std.stdio; void main() { int[] array = [1, 2, 3, 4]; int value = 2; fill(array, value); } when the only difference is the type, and the 'fill' method is meant to be generic? Thanks for your time. So I don't repeat excellent answer: http://stackoverflow.com/a/6401889/133707
Re: How to detect free/unfree memory segments?
I can just partially answer this part of the question: Could anybody explain what dangerous of memory fragmentation in languages without GC? Am I right understand that there is stay some small memory chunks that very hard to reuse? On Unix-like systems, system call for allocating memory is traditionally `brk`/`sbrk` which just moves "program break" which indicates how much memory program has allocated. Say that you're running your application, and your heap looks like this: 4096 --- | heap [myarr] | --- ^ | program break now you call `sbrk(2 * 4096)` as you need which will allocate 2*4096 for you: 4096 -- | heap [myarr][my new array] | -- ^ | program break Now, if you want to discard [myarr], it will leave a hole in heap, since you don't want to move `[my new array]`. This hole can be reused, but sometimes it can't, depending on the size, etc. That's why there's another system call you can use: mmap, which you would use to allocate space for `my new array`, non-contiguously and not adjacent to `myarr`, and so when you discard myarr, you don't have fragmentation issue: 4096 -- | heap [myarr] | | [my new array] | -- ^ | program break These, and many more facts (reusing freed area, when to use brk, when to use mmap, locking contention detection, for example) are used by the standard memory allocators you would use in non-GC program (such is malloc), so while it is possible that you will have memory fragmentation problems, it is very unlikely, unless you're writing something that's very different than vast majority of the programs. And in that case, there are different allocators that you can use, or you can write your own. Nemanja On Friday, 23 December 2016 at 06:18:02 UTC, Suliman wrote: I would like to visualize how GC works and display free/not free memory segments. How I can understand which of them are used and which not? Could anybody explain what dangerous of memory fragmentation in languages without GC? Am I right understand that there is stay some small memory chunks that very hard to reuse?
Re: Poll
What's in the `core.sys.posix.poll` is just a C wrapper, meaning if you use functions declared there, you're just calling the same one you would do in C, so it's very likely that you're doing something different in D and C program. Here's the example that works for me: ``` void main() { import core.stdc.stdio: fileno, stdin; import core.sys.posix.poll: poll, pollfd, POLLIN; import std.stdio: writeln, write, readln; import std.exception: enforce; pollfd fds; fds.fd = fileno(stdin); fds.events = POLLIN; for (;;) { int ret = poll(, 1, -1); switch (ret) { case POLLIN: auto data = readln(); write("Data on stdin: ", data); break; case 0: writeln("timeout"); break; case -1: default: enforce(false, "Error"); } } } ``` > If anyone has any suggestions, they would be much appreciated. Really all I need is a way of checking if a stream / file will block if I read it. Be very careful here, poll works on the readiness principle, and POSIX enforces that the regular files are always ready for reading and writing, meaning that this will not work. http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Regular files shall always poll TRUE for reading and writing. Nemanja On Friday, 23 December 2016 at 11:07:49 UTC, LeqxLeqx wrote: I've recently been attempting to detect whether or not stdin would block a read if I tried. I have attempted to use the /core/sys/posix/poll.d but that seems to be always returning '1' whether or not stdin has anything on it, whereas for the corresponding program written in C, it will return 1 only if there is something on stdin. If anyone has any suggestions, they would be much appreciated. Really all I need is a way of checking if a stream / file will block if I read it. Thanks
Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *
On Tuesday, 29 November 2016 at 15:30:33 UTC, Anders S wrote: Hi guys, I want to write into a fifo pipe using write( ...) Now a gather my data into my own struct IOREQ so in order to write I have to cast into an char buffer. My problem in dlang is that it doesn't accept the casting (IOREQ *) I get: Error: Cannot implicitly convert expression () of type char [1024] to IOREQ* define FC_GETSTATUS 501; typedef struct { SHORT fc; /* function code */ SHORT rs; /* return code */ INT size; /* size of this request, including header */ SHORT src;/* source */ INT argv[1];/* list of arguments */ } IOREQ; int WritePipe(int fd, int mess, int argc) { struct IOREQ * io; // my array of data char sbuf[1024]; io = (IOREQ *)buf; // Not accepted in dlang io.fc = FC_GETSTATUS; io.src = getpid();// works . // add more data st = write(fd, sbuf, 1024); // return st; } First, instead of: typedef struct { SHORT fc; /* function code */ SHORT rs; /* return code */ INT size; /* size of this request, including */ SHORT src;/* source */ INT argv[1];/* list of arguments */ } IOREQ; just struct IOREQ { SHORT fc; /* function code */ SHORT rs; /* return code */ INT size; /* size of this request, including */ SHORT src;/* source */ INT argv[1];/* list of arguments */ } IOREQ; Then, `write` has the following definition: https://github.com/dlang/druntime/blob/master/src/core/sys/posix/unistd.d#L100 `ssize_t write(int, in void*, size_t);` so, it accepts pointer to anything, no need to mess with `char[]`: IOREQ mystruct; mystruct.src = getpid(); // etc... // write it out write(fd, , mystruct.sizeof); // todo: Don't forget to check for // the return value, etc.
Re: Cannot implicitly convert expression () of type char [1024] to IOREQ *
On Tuesday, 29 November 2016 at 15:55:57 UTC, Nemanja Boric wrote: just struct IOREQ { SHORT fc; /* function code */ SHORT rs; /* return code */ INT size; /* size of this request, including */ SHORT src;/* source */ INT argv[1];/* list of arguments */ } IOREQ; Sorry, there's an extra IOREQ before and semicolon. struct IOREQ { SHORT fc; /* function code */ SHORT rs; /* return code */ INT size; /* size of this request, including */ SHORT src;/* source */ INT argv[1];/* list of arguments */ }
Re: not callable error
On Friday, 4 November 2016 at 14:37:04 UTC, bluphantom91 wrote: On Friday, 4 November 2016 at 02:59:49 UTC, Paul Backus wrote: On Friday, 4 November 2016 at 02:28:17 UTC, bluphantom91 wrote: Hello, I am trying to finish up a group project but I am running into a small problem. I keep getting an error about fgetc not being callable. The purpose of my program is to count the number of characters in a file. Any bit of help is appreciated! Here's my code: import std.stdio; import std.file; import std.string; Since you're using the C library stdio functions, you should import core.stdc.stdio ok I added that in, but now its giving me this: function core.stdc.stdio.getc (shared(_IO_FILE)* stream) is not callable using argument types (File) Am I just using getc the wrong way? You're mixing Phobos' `File` object and C's FILE stream. You're passing the `File` to getc which expects FILE. Also, if there's no specific reason, you should prefer the Phobos' file (which doesn't work with getc from C, as it would require obtaining the C stream from Phobos object, which would not be the best idea).
Re: Proper way to work around `Invalid memory operation`?
On Sunday, 25 September 2016 at 16:07:12 UTC, Matthias Klumpp wrote: At time, I work around this bug by calling close() manually at the appropriate time, but this feel like a rather poor solution. Cheers, Matthias That's not a poor solution, but rather a much better solution if you rely on GC to call destructor (which it may or may not do). Consider creating few hundreds instances of a class that holds several file descriptors and then throw them away in a short time (not unusual for tests, say). It is very likely that you'll hit file descriptors limits pretty soon.