Copy a struct and its context
I recently noticed nested struct capture its context by reference (which, BTW, is not mentioned at all here: https://dlang.org/spec/struct.html#nested). And bliting a struct obviously doesn't do a deep copy of its context. So my question is, is there a way to deep copy the context of a struct?
Re: std.process spawnShell/pipeShell dont capture output of the shell
I'm sorry for the "necro-bumping", but I've not found the solution for this yet, and i'm getting a different error message at the execution moment: "std.stdio.StdioException@/build/ldc/src/ldc/runtime/phobos/std/stdio.d(4066): Bad file descriptor" This is my testing code: module dd_test; import std.stdio, core.stdc.stdlib; import std.process, std.string; int main(string[] args) { string command_find = "(find /usr/share/icons/ -name a*) "; //string command_dd = "(dd if=/dev/urandom | pv -ptrbef -i 2 -s 2339876653 | dd of=/dev/null) 2>&1"; auto p = pipeShell(command_find, Redirect.all); foreach(str; p.stdin.byLine){ writefln("IN: %s", str); } foreach(str; p.stdout.byLine){ writefln("OUT: %s", str); } foreach(str; p.stderr.byLine){ writefln("Error: %s", str); } return 0; }
Building with dub fails on Ubuntu 16.10.
Hello, guys. I tried to build HelloWorld with dub, but i got strange linker error: Performing "debug" build using dmd for x86_64. test ~master: building configuration "application"... Linking... /usr/bin/ld: .dub/build/application-debug-linux.posix-x86_64-dmd_2071-0D6D3AB638EA28C55CFA241FFD9CA209/test.o: relocation R_X86_64_32 against symbol `__dmd_personality_v0' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libphobos2.a(object_15_594.o): relocation R_X86_64_32 against symbol `__dmd_personality_v0' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libphobos2.a(exception_223_55a.o): relocation R_X86_64_32 against symbol `__dmd_personality_v0' can not be used when making a shared object; recompile with -fPIC //manyStringsLater /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libphobos2.a(thread_26c_155.o): relocation R_X86_64_32 against symbol `__dmd_personality_v0' can not be used when making a shared object; recompile with -fPIC /usr/bin/ld: final link failed: Nonrepresentable section on output collect2: error: ld returned 1 exit status --- errorlevel 1 dmd failed with exit code 1. ld --version = 2.27 dmd --version = 2.071.1 dub --version = 1.0.0 Ubuntu 16.10 daily On previously versions of Ubuntu all work fine. What is the problem? Does anybody have any idea?
Re: cloning arrays
On Saturday, September 10, 2016 17:58:37 Russel Winder via Digitalmars-d-learn wrote: > What is the idiomatic way of cloning an array? > > I had wondered about x.dup but am now worried this is not creating a > shallow copy. dup creates an array with mutable elements that are copies of what was in the original array. How deep a copy that is depends on the type. It does exactly as deep a copy as simply copying the element would do. e.g. auto e = arr[4]; So, unless postblit constructors are involved, then it's not going to be doing any deep copying, and if a postblit constructor that does a deep copy is involved, then any other copying you'd do with it would be deep too unless you explicitly bit-blitted it rather than copying it, and that's usually a bad idea. - Jonathan M Davis
Re: Is it possible to override the behavior of a type when used in a conditional expression?
On Saturday, September 10, 2016 14:29:33 pineapple via Digitalmars-d-learn wrote: > On Saturday, 10 September 2016 at 14:24:23 UTC, ag0aep6g wrote: > > On 09/10/2016 04:10 PM, pineapple wrote: > >> I've got a struct and it would be very convenient if I could > >> specify > >> what happens when I write `if(value)` - is this possible? > > > > `if (value)` implies a cast to bool. Define opCast!bool and it > > gets called: > > > > > > struct S > > { > > > > bool opCast(T : bool)() { return true; } > > > > } > > void main() > > { > > > > S value; > > import std.stdio: writeln; > > if (value) writeln("yup"); > > > > } > > > > Huh, I could've sworn that some time ago I tried that and it > didn't work. Was this a recent addition to the language? No. That's how it's been for years now - certainly well before TDPL was released - and I suspect that it was that way in D1 (though I've never really used D1, so I'm not sure). - Jonathan M Davis
Re: Mutable class reference to immutable class
On Saturday, 10 September 2016 at 19:46:51 UTC, Jonathan Marler wrote: This is been bugging me for a while. Is it possible to have a mutable reference to an immutable class? [...] You are probably looking for std.typecons.Rebindable: https://dlang.org/phobos/std_typecons.html#.Rebindable
Mutable class reference to immutable class
This is been bugging me for a while. Is it possible to have a mutable reference to an immutable class? In other words, can you set a class variable to an immutable class, and then set that variable to another immutable class later? Mutable "slices" to immutable data are easy: immutable(char[]) x = "string for x"; // immutable data x immutable(char[]) y = "string for y"; // immutable data y immutable(char)[] mutableRef = x; // mutable slice to immutable data mutableRef = y; // OK, you can set mutableRef to another immutable slice But I can't figure out how to make a mutable "class" to immutable classes: immutable(SomeClass) x = new immutable SomeClass(); // immutable class x immutable(SomeClass) y = new immutable SomeClass(); // immutable class y immutable(SomeClass) mutableRef = x; mutableRef = y; // Error: cannot modify mutable expression mutableRef A workaround would be to make mutableRef an immutable(SomeClass)*, but this adds an extra level of indirection. Since all classes in D are pointers, x is already a pointer, so making a pointer to x would be making a pointer to a pointer that points to a class. It's obvious this issue is a result of the fact that all class variables are pointers. I don't suppose there is a way to represent a class's value type that I don't know about is there? SomeClass.ValueType? // Is there semantics for this I don't know about? If so, you could solve the problem by declaring mutableRef as: immmutable(SomeClass.ValueType)* mutableRef = x; I haven't encountered semantics for this anywhere in the language, but maybe someone else can enlighten me? If not, is there another way to get a mutable reference to an immutable class? Thanks in advance for the help.
cloning arrays
What is the idiomatic way of cloning an array? I had wondered about x.dup but am now worried this is not creating a shallow copy. I have seen people doing things with allocators, e.g. DStats – but am wondering if this is old code that should be excised. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Is it possible to override the behavior of a type when used in a conditional expression?
On 09/10/2016 04:29 PM, pineapple wrote: Was this a recent addition to the language? I don't think so.
Re: Is it possible to override the behavior of a type when used in a conditional expression?
On Saturday, 10 September 2016 at 14:24:23 UTC, ag0aep6g wrote: On 09/10/2016 04:10 PM, pineapple wrote: I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible? `if (value)` implies a cast to bool. Define opCast!bool and it gets called: struct S { bool opCast(T : bool)() { return true; } } void main() { S value; import std.stdio: writeln; if (value) writeln("yup"); } Huh, I could've sworn that some time ago I tried that and it didn't work. Was this a recent addition to the language?
Re: Is it possible to override the behavior of a type when used in a conditional expression?
On 09/10/2016 04:10 PM, pineapple wrote: I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible? `if (value)` implies a cast to bool. Define opCast!bool and it gets called: struct S { bool opCast(T : bool)() { return true; } } void main() { S value; import std.stdio: writeln; if (value) writeln("yup"); }
Re: inferred vs. annotated attributes
On 09/10/2016 10:23 AM, Q. Schroll wrote: Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure { } } void main() pure { X!uint mut = [ 1, 2 ]; // proves inference (cf. main is pure) // immutable X!uint imm1 = [ 1, 2 ]; // auto imm2 = immutable X!uint([1, 2]); } The commented lines yield error messages claiming the constructor cannot deduce function from argument types !()(int[]) immutable, however it can, if "pure" is explicitly annotated. Is this a bug or did I get something wrong about inference / unique expressions? Bug, or at least a very reasonable enhancement request. Inferred attributes should work just like explicit ones.
Is it possible to override the behavior of a type when used in a conditional expression?
I've got a struct and it would be very convenient if I could specify what happens when I write `if(value)` - is this possible?
Re: Reading hexidecimal from a file
On Saturday, 10 September 2016 at 12:18:22 UTC, Basile B. wrote: On Saturday, 10 September 2016 at 12:12:28 UTC, Basile B. wrote: On Saturday, 10 September 2016 at 12:04:08 UTC, Neurone wrote: Hi, I want to read a text file that contains sha1 hashes in hexidecimal, then convert the hashes back into ubyte[20]. Examples of some lines: E9785DC5 D43B5F67 F1B7D1CB 33279B7C 284E2593 04150E8F 1840BCA2 972BE1C5 2DE81039 0C486F9C How can I do this? The documentation for format strings is pretty dense, so couldn't understand most of it. at compile time you can do: import std.conv; enum array = hexString!(import(theFile)); the run-time version was proposed (https://github.com/dlang/phobos/pull/4487) but not interesting enough ;) No actually, Finally i would have worked, after cast(ubyte[]) hexString(stuff).array; fart...
Re: Reading hexidecimal from a file
On 11/09/2016 12:18 AM, Basile B. wrote: On Saturday, 10 September 2016 at 12:12:28 UTC, Basile B. wrote: On Saturday, 10 September 2016 at 12:04:08 UTC, Neurone wrote: Hi, I want to read a text file that contains sha1 hashes in hexidecimal, then convert the hashes back into ubyte[20]. Examples of some lines: E9785DC5 D43B5F67 F1B7D1CB 33279B7C 284E2593 04150E8F 1840BCA2 972BE1C5 2DE81039 0C486F9C How can I do this? The documentation for format strings is pretty dense, so couldn't understand most of it. at compile time you can do: import std.conv; enum array = hexString!(import(theFile)); the run-time version was proposed (https://github.com/dlang/phobos/pull/4487) but not interesting enough ;) No actually, it has nothing to do with you question! what you have to do is probably: split join chunck(2) to!ubyte sorry leaving now. Actually you'd want to filter out e.g. white space as well. So the long form via an input range would be: struct GetHex(T) { string from; T next; this(string input) { from = input; popFront; } @property { bool empty() { return from.length == 0 || next == 0; } T front() { return next; } } void popFront() { next = 0; char[T.sizeof * 2] got; ubyte count; while(count < got.length && from.length > 0) { got[count] = nextChar; if (got[count] != 0) count++; } import std.conv : parse; if (count > 0) { char[] temp = got[0 .. count]; next = parse!T(temp, 16); } } char nextChar() { char readIn = from[0]; from = from[1 .. $]; if ((readIn >= 'A' && readIn <= 'F') || (readIn >= 'a' && readIn <= 'f')) return readIn; else if (readIn >= '0' && readIn <= '9') return readIn; else return 0; } } void main() { string source = " E9785DC5 D43B5F67 F1B7D1CB 33279B7C 284E2593 04150E8F 1840BCA2 972BE1C5 2DE81039 0C486F9C"; import std.stdio : writeln; writeln(GetHex!uint(source)); }
Re: Templates problem
On Fri, 2016-09-09 at 12:22 +, Kagamin via Digitalmars-d-learn wrote: > On Thursday, 8 September 2016 at 10:26:04 UTC, Russel Winder > wrote: > > > > > > > > Do they use single assignment a lot? > > > > Python has no notion of single assignment. Exactly the > > opposite, Python allows everything to be changed at any time. > > Then you probably shouldn't pitch them alien concepts? Or maybe they need those alien concepts. Consider Python now has type signatures for functions and methods. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Templates problem
On Fri, 2016-09-09 at 14:20 +, jmh530 via Digitalmars-d-learn wrote: > […] > > What I mean is that Chapel doesn't have a lot of libraries (also > true for D, but things are getting better). If I'm going to do > some analysis, it usually takes much less time for me to do it in > R/Python/Matlab because they typically already have the libraries > that can do everything I need. This is very true: Python has the masses of things built on Python and NumPy today that make things easy to work with. My feeling is though that NumPy is not going to survive the next revolution in workstation and performance laptop hardware. Unless it absorbs the Chapel (or X10) PGAS model. The fastest way of doing this is to ditch the NumPy implementation and exchange it for either Chapel or something Chapel inspired. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Re: Reading hexidecimal from a file
On Saturday, 10 September 2016 at 12:04:08 UTC, Neurone wrote: Hi, I want to read a text file that contains sha1 hashes in hexidecimal, then convert the hashes back into ubyte[20]. Examples of some lines: E9785DC5 D43B5F67 F1B7D1CB 33279B7C 284E2593 04150E8F 1840BCA2 972BE1C5 2DE81039 0C486F9C How can I do this? The documentation for format strings is pretty dense, so couldn't understand most of it. at compile time you can do: import std.conv; enum array = hexString!(import(theFile)); the run-time version was proposed (https://github.com/dlang/phobos/pull/4487) but not interesting enough ;)
Re: Templates problem
On Thu, 2016-09-08 at 15:45 +, deXtoRious via Digitalmars-d-learn wrote: > […] > It's very early days for Chapel at the moment, but I don't really > see it as being remotely comparable to D or even Julia, it's much > closer to a DSL than a general purpose language. That's by no > means a bad thing, it seems like it could be a very useful tool > in a few years, but it's never going to completely substitute for > the likes of Python, C++ or D even for purely scientific > programming. I'm also a bit concerned about how limited the > compile time facilities seem there at the moment, but I guess > we'll just have to wait and see how it develops over the next > couple of years. In some sense Chapel is a 12 year old programming language, but clearly it is still a bit of a youngster in many ways. In that Chapel was developed to deal with programming supercomputers, it is a niche language for heavyweight computation. But that is exactly what Python is missing, and whilst NumPy, Numba, Cython, C, C++, Fortran, and D can help, none of them can make programming parallel systems quite as nice as a language specifically designed for the job. Chapel programming has a not dissimilar "feel" to D programming in many ways, it's just that Chapel is aimed at big kit, and D isn't. The trick though is that Chapel can be used on little kit as well and in this sense competes directly with D. The Chapel team are well funded, but are still focused on big kit, but are turning their attention to traction and little kit. In this sense Chapel could be a risk to D traction. Chapel focuses on arrays so it is very much a competitor to NumPy. Except that NumPy has many more years of things built on top of it than Chapel has. For programming computations for anything other than a small laptop, I'd choose Chapel over Python/NumPy any day. For visualising the results teh Python milieu wins hands down. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part
Reading hexidecimal from a file
Hi, I want to read a text file that contains sha1 hashes in hexidecimal, then convert the hashes back into ubyte[20]. Examples of some lines: E9785DC5 D43B5F67 F1B7D1CB 33279B7C 284E2593 04150E8F 1840BCA2 972BE1C5 2DE81039 0C486F9C How can I do this? The documentation for format strings is pretty dense, so couldn't understand most of it.
Re: inferred vs. annotated attributes
On Saturday, 10 September 2016 at 09:38:15 UTC, Lodovico Giaretta wrote: On Saturday, 10 September 2016 at 08:23:35 UTC, Q. Schroll wrote: Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure { } } void main() pure { X!uint mut = [ 1, 2 ]; // proves inference (cf. main is pure) // immutable X!uint imm1 = [ 1, 2 ]; // auto imm2 = immutable X!uint([1, 2]); } The commented lines yield error messages claiming the constructor cannot deduce function from argument types !()(int[]) immutable, however it can, if "pure" is explicitly annotated. Is this a bug or did I get something wrong about inference / unique expressions? A method (included ctors) that is not annotated const, nor immutable, nor inout, is implicitly mutable. This implies that your ctor is mutable, and as such it cannot create immutable instances. IIRC, a pure ctor can construct any objects (including immutable and shared). If you add another ctor marked immutable, everything works. An alternative is to mark the ctor as inout. Technically I know. The minimal example only explains the problem/bug. My real constructor does do things where I don't want inout attributes if I can avoid. I tested this solution and it works, but I'm not sure if it is by design. Sure. It did for me, too. The question is: Why does annotating an inferred attribute make a difference? (Or it doesn't and I don't see it all the time.)
Re: inferred vs. annotated attributes
On Saturday, 10 September 2016 at 08:23:35 UTC, Q. Schroll wrote: Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure { } } void main() pure { X!uint mut = [ 1, 2 ]; // proves inference (cf. main is pure) // immutable X!uint imm1 = [ 1, 2 ]; // auto imm2 = immutable X!uint([1, 2]); } The commented lines yield error messages claiming the constructor cannot deduce function from argument types !()(int[]) immutable, however it can, if "pure" is explicitly annotated. Is this a bug or did I get something wrong about inference / unique expressions? A method (included ctors) that is not annotated const, nor immutable, nor inout, is implicitly mutable. This implies that your ctor is mutable, and as such it cannot create immutable instances. If you add another ctor marked immutable, everything works. An alternative is to mark the ctor as inout. I tested this solution and it works, but I'm not sure if it is by design.
inferred vs. annotated attributes
Is there a difference between inferred and annotated attributes? Example: struct X(T) { this(S)(in S[] arr) // inferred pure { } } void main() pure { X!uint mut = [ 1, 2 ]; // proves inference (cf. main is pure) // immutable X!uint imm1 = [ 1, 2 ]; // auto imm2 = immutable X!uint([1, 2]); } The commented lines yield error messages claiming the constructor cannot deduce function from argument types !()(int[]) immutable, however it can, if "pure" is explicitly annotated. Is this a bug or did I get something wrong about inference / unique expressions?