Re: Non-blocking reads of a fifo (named pipe)?
On Monday, 3 December 2018 at 23:32:10 UTC, Anonymouse wrote: I have a fifo that I want to read lines from, but everything is blocking. [...] How can I go about doing this to get non-blocking reads? Or perhaps a way to test whether there is text waiting in the fifo? File.eof is not it. [...] I did this i think for my AsyncProcess class. It was a while ago and i never used it but IIRC it seemed to work on linux (not on Windows still IIRC) https://github.com/BBasile/iz/blob/master/import/iz/classes.d#L1584
Re: Non-blocking reads of a fifo (named pipe)?
On Mon, Dec 03, 2018 at 11:32:10PM +, Anonymouse via Digitalmars-d-learn wrote: > I have a fifo that I want to read lines from, but everything is > blocking. > > execute([ "mkfifo", filename ]); > File fifo = File(filename, "r"); // blocks already > immutable input = fifo.readln(); // blocks > foreach (line; fifo.byLineCopy) { /* blocks */ } > > How can I go about doing this to get non-blocking reads? Or perhaps a > way to test whether there is text waiting in the fifo? File.eof is not > it. > > if (fifo.hasData) > { > immutable stuff = fifo.readln(); > // ... > } I assume you're using Posix? In which case, you should take a look at the `select` or `epoll` system calls. Cf. core.sys.posix.sys.select. Or possibly `fcntl` (search for O_NONBLOCK), or call `open` directly with O_NONBLOCK. There's no common cross-platform way to do this, so generally you have to write your own code to handle it. T -- Music critic: "That's an imitation fugue!"
Non-blocking reads of a fifo (named pipe)?
I have a fifo that I want to read lines from, but everything is blocking. execute([ "mkfifo", filename ]); File fifo = File(filename, "r"); // blocks already immutable input = fifo.readln(); // blocks foreach (line; fifo.byLineCopy) { /* blocks */ } How can I go about doing this to get non-blocking reads? Or perhaps a way to test whether there is text waiting in the fifo? File.eof is not it. if (fifo.hasData) { immutable stuff = fifo.readln(); // ... }
Re: .dup vs operation on all elements
On Monday, 3 December 2018 at 22:04:25 UTC, Neia Neutuladh wrote: On Mon, 03 Dec 2018 21:27:52 +, faissaloo wrote: Then shouldn't the following output false, false, true? An object reference is a pointer value. The pointer values are copied. The pointed-at objects are not copied. Furthermore, the syntax Object[6] array = new Object(); only allocates one Object. Each item in the array is an object reference to the same object. Oh great, thanks for clearing this up. I also didn't know about shorthand only allocating one object (the code he shared was a piece of code we were testing with which I mistakenly wrote!) Thanks!
Re: .dup vs operation on all elements
On Mon, 03 Dec 2018 21:27:52 +, faissaloo wrote: > Then shouldn't the following output false, false, true? An object reference is a pointer value. The pointer values are copied. The pointed-at objects are not copied. Furthermore, the syntax Object[6] array = new Object(); only allocates one Object. Each item in the array is an object reference to the same object.
Re: .dup vs operation on all elements
On Monday, 3 December 2018 at 20:37:22 UTC, Jonathan M Davis wrote: On Monday, December 3, 2018 1:07:24 PM MST Goksan via Digitalmars-d-learn wrote: Are there any differences between these 2 methods of copying elements? double[] array = [ 1, 20, 2, 30, 7, 11 ]; // Non dup double[6] bracket_syntax_dup = array; bracket_syntax_dup[] = array; bracket_syntax_dup[0] = 50; // Dup double[6] normal_dup = array.dup; normal_dup[0] = 100; OUTPUT: (array, bracket_syntax_dup and normal_dup respectively): [1, 20, 2, 30, 7, 11] [50, 20, 2, 30, 7, 11] [100, 20, 2, 30, 7, 11] dup allocates a new dynamic array and copies the elements of the existing dynamic array to the new one. Calling dup in order to assign to a static array is just needlessly allocating a dynamic array. The contents of the array are going to be copied to the static array regardless, but instead of just copying the elements, if you use dup, you're allocating a new dynamic array, copying the elements into that dynamic array, and then you're copying the elements into the static array. There's no point. You use dup when you want to copy the elements of a dynamic array instead of simply slicing it. Slicing gives you a new dynamic array that points to exactly the same elements. It's just copying the pointer and the length (meaning that mutating the elements of the new slice will affect the elements in the original array), whereas dup actually allocates a new block of memory for the new dynamic array to be a slice of (copying the elements over in the process), so mutating the elements in the new dynamic array then won't affect the elements in the original. Regardless, when you create a static array, it's not a slice of anything (since its elements sit directly on the stack), and when assign to it, you're simply copying the elements over. - Jonathan M Davis Then shouldn't the following output false, false, true? import std.stdio; class Programmer { bool is_confused = false; void setConfusion(bool confusion_status) { is_confused = confusion_status; } } void main() { Programmer[6] array = new Programmer(); Programmer[6] bracket_syntax_dup = array; bracket_syntax_dup[] = array; Programmer[6] normal_dup = array.dup; normal_dup[0].setConfusion(true); bracket_syntax_dup[0] = new Programmer(); writeln(array[0].is_confused); writeln(bracket_syntax_dup[0].is_confused); writeln(normal_dup[0].is_confused); }
Re: .dup vs operation on all elements
On Monday, December 3, 2018 1:07:24 PM MST Goksan via Digitalmars-d-learn wrote: > Are there any differences between these 2 methods of copying > elements? > > double[] array = [ 1, 20, 2, 30, 7, 11 ]; > > // Non dup > double[6] bracket_syntax_dup = array; > bracket_syntax_dup[] = array; > bracket_syntax_dup[0] = 50; > > // Dup > double[6] normal_dup = array.dup; > normal_dup[0] = 100; > > OUTPUT: (array, bracket_syntax_dup and normal_dup respectively): > [1, 20, 2, 30, 7, 11] > [50, 20, 2, 30, 7, 11] > [100, 20, 2, 30, 7, 11] dup allocates a new dynamic array and copies the elements of the existing dynamic array to the new one. Calling dup in order to assign to a static array is just needlessly allocating a dynamic array. The contents of the array are going to be copied to the static array regardless, but instead of just copying the elements, if you use dup, you're allocating a new dynamic array, copying the elements into that dynamic array, and then you're copying the elements into the static array. There's no point. You use dup when you want to copy the elements of a dynamic array instead of simply slicing it. Slicing gives you a new dynamic array that points to exactly the same elements. It's just copying the pointer and the length (meaning that mutating the elements of the new slice will affect the elements in the original array), whereas dup actually allocates a new block of memory for the new dynamic array to be a slice of (copying the elements over in the process), so mutating the elements in the new dynamic array then won't affect the elements in the original. Regardless, when you create a static array, it's not a slice of anything (since its elements sit directly on the stack), and when assign to it, you're simply copying the elements over. - Jonathan M Davis
Re: .dup vs operation on all elements
Yes it is. The dup version just make an extra copy of array for no reason. po 3. 12. 2018 21:10 odesílatel Goksan via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> napsal: > Are there any differences between these 2 methods of copying > elements? > > double[] array = [ 1, 20, 2, 30, 7, 11 ]; > > // Non dup > double[6] bracket_syntax_dup = array; > bracket_syntax_dup[] = array; > bracket_syntax_dup[0] = 50; > > // Dup > double[6] normal_dup = array.dup; > normal_dup[0] = 100; > > OUTPUT: (array, bracket_syntax_dup and normal_dup respectively): > [1, 20, 2, 30, 7, 11] > [50, 20, 2, 30, 7, 11] > [100, 20, 2, 30, 7, 11] >
.dup vs operation on all elements
Are there any differences between these 2 methods of copying elements? double[] array = [ 1, 20, 2, 30, 7, 11 ]; // Non dup double[6] bracket_syntax_dup = array; bracket_syntax_dup[] = array; bracket_syntax_dup[0] = 50; // Dup double[6] normal_dup = array.dup; normal_dup[0] = 100; OUTPUT: (array, bracket_syntax_dup and normal_dup respectively): [1, 20, 2, 30, 7, 11] [50, 20, 2, 30, 7, 11] [100, 20, 2, 30, 7, 11]
Re: File .. byLine
On 12/3/18 2:03 AM, Joel wrote: On Monday, 3 December 2018 at 06:55:50 UTC, Nicholas Wilson wrote: On Monday, 3 December 2018 at 06:09:21 UTC, Joel wrote: [...] https://run.dlang.io/is/h0ArAB works for me. If you want it as a string not char[] then byLineCopy should work, if not just `.idup` `line`. Oh, I was using std.algorithm's 'stripLeft' instead of std.string's 'stripLeft'. Interesting. I can see the reason too -- there is no "should be stripped" property for all types, just character types (is it a space). -Steve
Re: Initialize static array without explicit length
On Monday, 3 December 2018 at 12:19:26 UTC, Dennis wrote: On Monday, 3 December 2018 at 10:00:31 UTC, Simen Kjærås wrote: However, it's easy to implement in a library: It even is in phobos: https://dlang.org/phobos/std_array.html#.staticArray ``` import std.array: staticArray; auto a = [0, 1, 2].staticArray; ``` The more you know. Thankies! :) -- Simen
Re: Initialize static array without explicit length
On Monday, 3 December 2018 at 10:00:31 UTC, Simen Kjærås wrote: However, it's easy to implement in a library: It even is in phobos: https://dlang.org/phobos/std_array.html#.staticArray ``` import std.array: staticArray; auto a = [0, 1, 2].staticArray; ```
Re: Why does nobody seem to think that `null` is a serious problem in D?
On 22.11.18 16:19, Steven Schveighoffer wrote: In terms of language semantics, I don't know what the right answer is. If we want to say that if an optimizer changes program behavior, the code must be UB, then this would have to be UB. But I would prefer saying something like -- if a segfault occurs and the program continues, the system is in UB-land, but otherwise, it's fine. If this means an optimized program runs and a non-optimized one crashes, then that's what it means. I'd be OK with that result. It's like Schrodinger's segfault! I don't know what it means in terms of compiler assumptions, so that's where my ignorance will likely get me in trouble :) This is called nondeterministic semantics, and it is a good idea if you want both efficiency and memory safety guarantees, but I don't know how well our backends would support it. (However, I think it is necessary anyway, e.g. to give semantics to pure functions.)
Re: Initialize static array without explicit length
On Monday, 3 December 2018 at 09:51:45 UTC, Andrey wrote: Hi, I want to create a static array and immediately init it with values: uint[x] data = [1,3,10,44,0,5000]; I don't want to set the length of it explicitly (x in square brackets). I want that compiler itself counted number of values (in example it is 6). What should be a right syntax? There's no special syntax for this (even though it's been requested numerous times). However, it's easy to implement in a library: import std.traits : CommonType; CommonType!T[T.length] staticArray(T...)(T args) if (is(CommonType!T)) { return [args]; } unittest { auto a = staticArray(1,2,3,4); static assert(is(typeof(a) == int[4])); } -- Simen
Initialize static array without explicit length
Hi, I want to create a static array and immediately init it with values: uint[x] data = [1,3,10,44,0,5000]; I don't want to set the length of it explicitly (x in square brackets). I want that compiler itself counted number of values (in example it is 6). What should be a right syntax?