Re: -L--demangle=dlang doesn't work
On Saturday, 6 January 2018 at 05:44:28 UTC, Venkat wrote: Why does gcc say "unknown demangling style `dlang'" ? Do I need GDC for demangling to work ? 85198AB7DE24894B5F742FBD5/libvibe-d_data.a /home/venkat/.dub/packages/vibe-d-0.8.2/vibe-d/utils/.dub/build/library-unittest-linux.posix-x86_64-dmd_2077-B9AE30DD34FDC5ADDE81E208F10DF014/libvibe-d_utils.a -L--no-as-needed -L--demangle=dlang -L-lsqlite3 -L-levent_pthreads -L-levent -L-lssl -L-lcrypto -L-ldl -g /usr/bin/ld: unknown demangling style `dlang' collect2: error: ld returned 1 exit status Error: linker exited with status 1 FAIL .dub/build/webmarx-test-application-unittest-linux.posix-x86_64-dmd_2077-C5DB1ABAED0A1191C5B2ACAFDC70EAC6/ webmarx-test-application executable dmd failed with exit code 1. Full exception: object.Exception@source/dub/compilers/compiler.d(115): dmd failed with exit code 1. I'm not familiar with that flag maybe you have to register ddmangle with it? But failing all else you can pipe the output through ddmangle.
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 18:00:34 UTC, thedeemon wrote: On Friday, 5 January 2018 at 17:59:32 UTC, thedeemon wrote: Tuple!( staticMap!(Arr, ColumnTypes) ) res; // array of tuples Sorry, I meant tuple of arrays, of course. Hi Deemon, Thank you very much, I tested your code, initially the code did not produce the expected output, and found an issue in the the key line of code as below, after updating the output was as expected. Can you please let me know how to change the array from standard array to container array. auto ks = col.map!(v => col.countUntil(v)).array; // Your code(col.countUntil) auto ks = col.map!(v => vals.countUntil(v)).array; // Changed code(vals.countUntil) From, Vino.B
-L--demangle=dlang doesn't work
Why does gcc say "unknown demangling style `dlang'" ? Do I need GDC for demangling to work ? 85198AB7DE24894B5F742FBD5/libvibe-d_data.a /home/venkat/.dub/packages/vibe-d-0.8.2/vibe-d/utils/.dub/build/library-unittest-linux.posix-x86_64-dmd_2077-B9AE30DD34FDC5ADDE81E208F10DF014/libvibe-d_utils.a -L--no-as-needed -L--demangle=dlang -L-lsqlite3 -L-levent_pthreads -L-levent -L-lssl -L-lcrypto -L-ldl -g /usr/bin/ld: unknown demangling style `dlang' collect2: error: ld returned 1 exit status Error: linker exited with status 1 FAIL .dub/build/webmarx-test-application-unittest-linux.posix-x86_64-dmd_2077-C5DB1ABAED0A1191C5B2ACAFDC70EAC6/ webmarx-test-application executable dmd failed with exit code 1. Full exception: object.Exception@source/dub/compilers/compiler.d(115): dmd failed with exit code 1.
Re: Templates for DRY code
On Saturday, 6 January 2018 at 03:08:19 UTC, Ali Çehreli wrote: It's hard to find a balance between fully explicit and fully automatic. I find myself going back and forth between those two extremes. Ali Code is something that humans write and read (and read far more than write). So I prefer to optimise for mental processing ;-) By this, I mean reducing the amount of information I need to chunk, to make sense of something (whether that be writing, or reading). e.g double[] a; string s = "1.2"; a.append(s) requires me to go off and discover what append is doing, since 'append' is clearly not correctly describing what is actually going on here, in this little chunk. So, now I have to go off and discover the extra chunk I need, in order to make sense of this little chunk. Suddenly, the chunk has become a lot larger than it needed to be. so yeah, a simple rename of append would help.. or even.. a.append( s.to!ConvertToElementType(a) ); That's not valid code of course, but the semantics are all contained in that single chunk.
Re: Templates for DRY code
On 01/05/2018 06:14 PM, codephantom wrote: > On Saturday, 6 January 2018 at 01:33:11 UTC, Ali Çehreli wrote: >> One solution is to wrap ~= in a function template. Now the conversion >> is automatically made to the element type of the array: >> ... >> . >> I think append() could be a part of std.array but I can't find >> anything like that in Phobos. >> >> Ali > > The problem with this, in my opinion, is that your template 'append' is > doing a conversion behind the scenes..ie. I wouldn't know that 'append' > actually means 'convert & then append'. > > When I read: > > double[] a; > string s = "1.2"; > a.append(s); > > I think to myself...wtf is going on here? How can you append a string to > an array of doubles? > > That's when I'd have go and find the append template and work out what > it is really doing. > I agree with your point as well. A better name can help there a little. It's hard to find a balance between fully explicit and fully automatic. I find myself going back and forth between those two extremes. Ali
Re: Templates for DRY code
On Saturday, 6 January 2018 at 01:33:11 UTC, Ali Çehreli wrote: One solution is to wrap ~= in a function template. Now the conversion is automatically made to the element type of the array: ... . I think append() could be a part of std.array but I can't find anything like that in Phobos. Ali The problem with this, in my opinion, is that your template 'append' is doing a conversion behind the scenes..ie. I wouldn't know that 'append' actually means 'convert & then append'. When I read: double[] a; string s = "1.2"; a.append(s); I think to myself...wtf is going on here? How can you append a string to an array of doubles? That's when I'd have go and find the append template and work out what it is really doing.
Templates for DRY code
Nothing new here... I'm just reminded of how templates can help with DRY (don't repeat yourself) code. Here is a piece of code that uses an alias to be flexible on the element type: import std.conv; alias MyType = double;// <-- Nice, MyType is used twice below void main() { MyType[] a; // ... string s = "1.2"; a ~= s.to!MyType; // <-- This usage is troubling } Although to!MyType looks like a good idea there, the two usages of MyType must be kept in sync manually. Why should we repeat MyType when it's already known to be ElementType!(typeof(a)) anyway. (Unfortunately, that syntax is not very clear in code.) One solution is to wrap ~= in a function template. Now the conversion is automatically made to the element type of the array: import std.conv; void append(T, S)(ref T[] arr, S s) { arr ~= s.to!T; } void main() { double[] a;// <-- Removed MyType alias as well // ... string s = "1.2"; a.append(s); } In simple cases like this, one may not even need the alias. 'double' appears in just one place there. I think append() could be a part of std.array but I can't find anything like that in Phobos. Ali
Re: what's the scope of a static variable inside a local function?
On Friday, January 05, 2018 17:17:47 Steven Schveighoffer via Digitalmars-d- learn wrote: > On 1/5/18 5:07 PM, Jonathan M Davis wrote: > > but doesn't print out anything about main or f. I don't know if that's a > > bug or not, since the only way that I can think of to make it work > > would be to have the compiler invisibly add a static destructor to the > > module to clean them up, and that would cause other problems. It's just > > not something that I've ever thought about before. > > Hm... if you counted on thread termination to clean up a resource, then > it's definitely a bug. > > Static dtors would be the most effective way to clean it up, as the > mechanism already exists, but they would have to be marked as > independent. And of course, -betterC couldn't do it, which means RAII > would be broken when compiled that way. Yeah, so I don't know. Digging around on bugzilla though, it looks like a similar problem was already reported for module-level variables, and Andrei commented that it didn't work with static variables either but wasn't sure what the spec said on the matter: https://issues.dlang.org/show_bug.cgi?id=14650 - Jonathan M Davis
Re: Upcasting slice of class elements
On 1/5/18 6:04 PM, H. S. Teoh wrote: On Fri, Jan 05, 2018 at 10:16:04PM +, Nordlöw via Digitalmars-d-learn wrote: Why isn't class X {} class Y : X {} X[] xs = cast(X[])(Y[].init); compilable in safe D? What's unsafe about such a cast? Your original code snippet seems redundant. If you wanted an empty array of X's, there's no need to cast it from an array of Y's. Perhaps you had something like this in mind instead?: X[] xs = [ cast(X) new Y(...), new Y(...), ... ]; The cast is only needed for the first element; common type inference takes care of the rest of the array. Hm... I tried just this and it works: X[] xs = [new Y]; I think for non-class types it may need more casting. -Steve
Re: Upcasting slice of class elements
On Fri, Jan 05, 2018 at 10:16:04PM +, Nordlöw via Digitalmars-d-learn wrote: > Why isn't > > class X {} > class Y : X {} > X[] xs = cast(X[])(Y[].init); > > compilable in safe D? > > What's unsafe about such a cast? Your original code snippet seems redundant. If you wanted an empty array of X's, there's no need to cast it from an array of Y's. Perhaps you had something like this in mind instead?: X[] xs = [ cast(X) new Y(...), new Y(...), ... ]; The cast is only needed for the first element; common type inference takes care of the rest of the array. T -- Give a man a fish, and he eats once. Teach a man to fish, and he will sit forever.
Re: Upcasting slice of class elements
On 1/5/18 5:16 PM, Nordlöw wrote: Why isn't class X {} class Y : X {} X[] xs = cast(X[])(Y[].init); compilable in safe D? Hm... given that there is no other reference to xs, it should work. But obviously you have a different example in mind, as this makes no sense? This works: X[] xs = [new Y]; Which really isn't any different. What's unsafe about such a cast? In general: Y[] ys = ...; xs = cast(X[])ys; xs[0] = new X; ys[0].foo; // oops, ys[0] isn't really a Y any more. But in your specific case, I think the compiler should see that it's ok. -Steve
Re: Upcasting slice of class elements
On Friday, 5 January 2018 at 22:16:04 UTC, Nordlöw wrote: Why isn't class X {} class Y : X {} X[] xs = cast(X[])(Y[].init); compilable in safe D? What's unsafe about such a cast? class X {} class Y : X {} class Z : X {} Y[] ys = Y[].init; X[] xs = cast(X[])(ys); xs[0] = new Z; What happens to ys[0] there?
Re: what's the scope of a static variable inside a local function?
On 1/5/18 5:07 PM, Jonathan M Davis wrote: but doesn't print out anything about main or f. I don't know if that's a bug or not, since the only way that I can think of to make it work would be to have the compiler invisibly add a static destructor to the module to clean them up, and that would cause other problems. It's just not something that I've ever thought about before. Hm... if you counted on thread termination to clean up a resource, then it's definitely a bug. Static dtors would be the most effective way to clean it up, as the mechanism already exists, but they would have to be marked as independent. And of course, -betterC couldn't do it, which means RAII would be broken when compiled that way. -Steve
Upcasting slice of class elements
Why isn't class X {} class Y : X {} X[] xs = cast(X[])(Y[].init); compilable in safe D? What's unsafe about such a cast?
Re: what's the scope of a static variable inside a local function?
On Friday, January 05, 2018 16:59:38 Steven Schveighoffer via Digitalmars-d- learn wrote: > On 1/5/18 4:44 PM, Marc wrote: > >> void foo() { > >> void baa() { > >> static int n; > >> writeln(n++); > >> } > >> } > >> > >> void main() { > >> static int x; > >> foo(); > >> doSomething(x); > >> } > > > > does x and n has same lifetime, i.e, program's execution or n is longer > > available onde foo() call reach out of scope? > > Both are the same. The lifetime is actually thread-local (one instance > per thread). Yeah, they're initialized at compile-time (unlike what you get in C++), which means that you don't get all of that stuff about them being created when the function is first called. And they live as long as the thread lives. What I started wondering about when I saw this though was when the destructors for local static variables get run, and it turns out that they don't. This code --- import std.stdio; struct S { this(string foo) { _foo = foo; } ~this() { writefln("%s destroyed", _foo); } string _foo; } void main() { static mainStatic = S("main"); auto s = S("local"); f(); } void f() { static fStatic = S("f"); } --- prints out local destroyed but doesn't print out anything about main or f. I don't know if that's a bug or not, since the only way that I can think of to make it work would be to have the compiler invisibly add a static destructor to the module to clean them up, and that would cause other problems. It's just not something that I've ever thought about before. - Jonathan M Davis
Re: Help optimizing UnCompress for gzipped files
On 1/5/18 3:09 PM, Christian Köstlin wrote: On 05.01.18 15:39, Steven Schveighoffer wrote: Yeah, I guess most of the bottlenecks are inside libz, or the memory allocator. There isn't much optimization to be done in the main program itself. D compiles just the same as C. So theoretically you should be able to get the same performance with a ported version of your C code. It's worth a shot. I added another version that tries to do the "same" as the c version using mallocator, but i am still way off, perhaps its creating too many ranges on the underlying array. but its around the same speed as your great iopipe thing. Hm... I think really there is some magic initial state of the allocator, and that's what allows it to go so fast. One thing about the D version, because druntime is also using malloc (the GC is backed by malloc'd data after all), the initial state of the heap is quite different from when you start in C. It may be impossible or nearly impossible to duplicate the performance. But the flipside (if this is indeed the case) is that you won't see the same performance in a real-world app anyway, even in C. One thing to try, you preallocate the ENTIRE buffer. This only works if you know how many bytes it will decompress to (not always possible), but it will take the allocator out of the equation completely. And it's probably going to be the most efficient method (you aren't leaving behind smaller unused blocks when you realloc). If for some reason we can't beat/tie the C version doing that, then something else is going on. My solution does have the same memory leak, as I am not sure how to best get the memory out of the FastAppender so that it is automagically cleaned up. Perhaps if we get rc things, this gets easier? I've been giving some thought to this. I think iopipe needs some buffer management primitives that allow you to finagle the buffer. I've been needing this for some time anyway (for file seeking). Right now, the buffer itself is buried in the chain, so it's hard to get at the actual buffer. Alternatively, I probably also need to give some thought to a mechanism that auto-frees the memory when it can tell nobody is still using the iopipe. Given that iopipe's signature feature is direct buffer access, this would mean anything that uses such a feature would have to be unsafe. -Steve
Re: what's the scope of a static variable inside a local function?
On 1/5/18 4:44 PM, Marc wrote: void foo() { void baa() { static int n; writeln(n++); } } void main() { static int x; foo(); doSomething(x); } does x and n has same lifetime, i.e, program's execution or n is longer available onde foo() call reach out of scope? Both are the same. The lifetime is actually thread-local (one instance per thread). -Steve
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 15:38:17 UTC, Binghoo Dang wrote: On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: ~~ struct User { int id; string name; string email; } class ORM { } auto db = new ORM; auto users = db.select(User).where(email.like("*@hotmail.com")).limit(10); foreach(user; users) { writeln("user: " + user.name + "\n"); } ~~ this rust code is support it: https://github.com/diesel-rs/diesel/blob/master/examples/postgres/all_about_updates/src/lib.rs there's entity library exist in D Dub, which is an ORM framework, you may read the code for reference: https://code.dlang.org/packages/entity Yes, [entity] is my team's project, I want it to be simpler :)
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 12:19:11 UTC, thedeemon wrote: On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: db.select(User).where(email.like("*@hotmail.com")).limit(10); You need to read about templates in D, here's a good guide: https://github.com/PhilippeSigaud/D-templates-tutorial Basically you can write a function like auto select(Class, string fieldName)(Class value) { ... and call it later as select!(User, "name")(u); Here User and "name" are compile-time arguments, you can pass there types, values and more exotic stuff like other templates. And inside the function you can use __traits(getMember, u, fieldName) to get field by its name from the passed value. See: https://dlang.org/spec/traits.html I can try it?: auto users = db.select!(User).where(User.email.like("*@hotmail.com")).limit(10);
what's the scope of a static variable inside a local function?
void foo() { void baa() { static int n; writeln(n++); } } void main() { static int x; foo(); doSomething(x); } does x and n has same lifetime, i.e, program's execution or n is longer available onde foo() call reach out of scope?
Re: Help optimizing UnCompress for gzipped files
On 05.01.18 15:39, Steven Schveighoffer wrote: > Yeah, I guess most of the bottlenecks are inside libz, or the memory > allocator. There isn't much optimization to be done in the main program > itself. > > D compiles just the same as C. So theoretically you should be able to > get the same performance with a ported version of your C code. It's > worth a shot. I added another version that tries to do the "same" as the c version using mallocator, but i am still way off, perhaps its creating too many ranges on the underlying array. but its around the same speed as your great iopipe thing. My solution does have the same memory leak, as I am not sure how to best get the memory out of the FastAppender so that it is automagically cleaned up. Perhaps if we get rc things, this gets easier? I updated: https://github.com/gizmomogwai/benchmarks/tree/master/gunzip with the newest numbers on my machine, but I think your iopipe solution is the best one we can get at the moment! >> rust is doing quite well there > > I'll say a few words of caution here: > > 1. Almost all of these tests use the same C library to unzip. So it's > really not a test of the performance of decompression, but the > performance of memory management. And it appears that any test using > malloc/realloc is in a different tier. Presumably because of the lack of > copies (as discussed earlier). > 2. Your rust test (I think, I'm not sure) is testing 2 things in the > same run, which could potentially have dramatic consequences for the > second test. For instance, it could already have all the required memory > blocks ready, and the allocation strategy suddenly gets better. Or maybe > there is some kind of caching of the input being done. I think you have > a fairer test for the second option by running it in a separate program. > I've never used rust, so I don't know what exactly your code is doing. > 3. It's hard to make a decision based on such microbenchmarks as to > which solution is "better" in an actual real-world program, especially > when the state/usage of the memory allocator plays a huge role in this. sure .. thats true
Re: Consequences of casting away immutable from pointers
On Friday, 5 January 2018 at 18:13:11 UTC, H. S. Teoh wrote: On Fri, Jan 05, 2018 at 05:50:34PM +, jmh530 via Digitalmars-d-learn wrote: Be careful with that: class C { int x; } immutable C c = new C(5); auto i = c.x; C y = cast(C) c; y.x = 10; i = c.x; // <-- compiler may assume c.x is still 5 Since c.x is read from an immutable object, the compiler may assume that its value hasn't changed the second time you access it, so it may just elide the second assignment to i completely, thereby introducing a bug into the code. Basically, casting away immutable is UB, and playing with UB is playing with fire. :-P And these things are nasty. We had one in our C project last month that had us tear our hair out. It was in the end a documentation problem of gcc that induced the misunderstanding of the purpose of __attribut__((malloc)) and its effect on aliased pointer.
Re: Consequences of casting away immutable from pointers
On Fri, Jan 05, 2018 at 05:50:34PM +, jmh530 via Digitalmars-d-learn wrote: > On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: > > > > Yes, this is undefined behavior. > > > > https://dlang.org/spec/const3.html#removing_with_cast > > > > The compiler assumes x is going to be 5 forever, so instead of > > loading the value at that address, it just loads 5 into a register > > (or maybe it just folds x == 5 into true). > > > > The compiler would likely be free to assume *p_x == 5 forever also, > > if it was clever enough. > > > > I'd recommend not doing this. > > > > -Steve > > I also checked that if you create an instance of a class on the heap > with an immutable constructor, then it's no longer in the register. > Thus, I can now modify the immutable object from the pointer that I > casted away immutable (though not that I would!) Be careful with that: class C { int x; } immutable C c = new C(5); auto i = c.x; C y = cast(C) c; y.x = 10; i = c.x; // <-- compiler may assume c.x is still 5 Since c.x is read from an immutable object, the compiler may assume that its value hasn't changed the second time you access it, so it may just elide the second assignment to i completely, thereby introducing a bug into the code. Basically, casting away immutable is UB, and playing with UB is playing with fire. :-P T -- Береги платье снову, а здоровье смолоду.
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 17:59:32 UTC, thedeemon wrote: Tuple!( staticMap!(Arr, ColumnTypes) ) res; // array of tuples Sorry, I meant tuple of arrays, of course.
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 17:50:13 UTC, thedeemon wrote: Here's my version of solution. I've used ordinary arrays instead of std.container.array, since the data itself is in GC'ed heap anyway. I used csv file separated by tabs, so told csvReader to use '\t' for delimiter. And since lines of the file are copied to heap anyway, it's easier to skip unnecessary line splitting and joining and do the reading simpler: import std.file : readText; auto readData(string fname) { Tuple!( staticMap!(Arr, ColumnTypes) ) res; // array of tuples foreach (record; fname.readText.csvReader!(Tuple!ColumnTypes)('\t')) foreach(i, T; ColumnTypes) res[i] ~= record[i]; return res; }
Re: Consequences of casting away immutable from pointers
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: Yes, this is undefined behavior. https://dlang.org/spec/const3.html#removing_with_cast The compiler assumes x is going to be 5 forever, so instead of loading the value at that address, it just loads 5 into a register (or maybe it just folds x == 5 into true). The compiler would likely be free to assume *p_x == 5 forever also, if it was clever enough. I'd recommend not doing this. -Steve I also checked that if you create an instance of a class on the heap with an immutable constructor, then it's no longer in the register. Thus, I can now modify the immutable object from the pointer that I casted away immutable (though not that I would!)
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 13:09:25 UTC, Vino wrote: Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written. Hi, I am trying to implement data dictionary compression, and below is the function of the program, Function read: This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data. CSV file content: Miller America 23 JohnIndia 42 Baker Australia 21 Zsuwalski Japan 45 Baker America 45 Miller India 23 Function Main This function receives the data from the function read. Creates an array based of the function return type – ( typeof(read()[i]) Data ); Sorts the data and removes the duplicates and stores the data in the above array. Then using “countUntil” function we can accomplish the data dictionary compression. Thank you for the explanation, this is a nice little task. Here's my version of solution. I've used ordinary arrays instead of std.container.array, since the data itself is in GC'ed heap anyway. I used csv file separated by tabs, so told csvReader to use '\t' for delimiter. import std.algorithm: countUntil, joiner, sort, uniq, map; import std.csv: csvReader; import std.stdio: File, writeln; import std.typecons: Tuple, tuple; import std.meta; import std.array : array; //we know types of columns, so let's state them once alias ColumnTypes = AliasSeq!(string, string, int); alias Arr(T) = T[]; auto readData() { auto file = File("data.csv", "r"); Tuple!( staticMap!(Arr, ColumnTypes) ) res; // tuple of arrays foreach (record; file.byLineCopy.joiner("\n").csvReader!(Tuple!ColumnTypes)('\t')) foreach(i, T; ColumnTypes) res[i] ~= record[i]; // here res[i] can have different types return res; } //compress a single column auto compress(T)(T[] col) { T[] vals = sort(col.dup[]).uniq.array; auto ks = col.map!(v => col.countUntil(v)).array; return tuple(vals, ks); } void main() { auto columns = readData(); foreach(i, ColT; ColumnTypes) { // here the data can have different type for different i auto vk = compress(columns[i]); writeln(vk[0][]); //output data, you can write files here writeln(vk[1][]); //output indices } }
Re: Array is already defined
On Friday, 5 January 2018 at 16:55:50 UTC, Steven Schveighoffer wrote: On 1/5/18 11:39 AM, Vino wrote: Hi Steve, if we add the braces we are getting the Error: undefined identifier Datacol void main () { Array!int Keycol; static foreach(i; 0 .. 3) { { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } } writeln (Datacol[], Keycol[]); } That's because writeln is outside the loop that defines Datacol. Without knowing what you are trying to do, it's hard to further help. -Steve Hi Steve, Thank you very much, was able to resolve the issue after adding the writeln within the braces, I am trying to implement a data dictionary compressing using D, and had 2 issue and this is one of the issue, and the other issue is detailed in the post "Error: variable i cannot be read at compile time", which contains the details of this program, if possible can you please help on on the other issue in the post "Error: variable i cannot be read at compile time". From, Vino.B
Re: Array is already defined
On 1/5/18 11:39 AM, Vino wrote: Hi Steve, if we add the braces we are getting the Error: undefined identifier Datacol void main () { Array!int Keycol; static foreach(i; 0 .. 3) { { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } } writeln (Datacol[], Keycol[]); } That's because writeln is outside the loop that defines Datacol. Without knowing what you are trying to do, it's hard to further help. -Steve
Re: Array is already defined
On Friday, 5 January 2018 at 16:07:49 UTC, Steven Schveighoffer wrote: On 1/5/18 10:56 AM, Vino wrote: On Friday, 5 January 2018 at 15:28:58 UTC, Jonathan M Davis wrote: On Friday, January 05, 2018 15:22:49 Vino via Digitalmars-d-learn wrote: Hi All, Request your help on how to unset or delete an array, in the below example, we get an error "Common is already defined". Auto fn1 () { Array!string Text; Array!string Number; return tuple(Text, Number); } Void main () { static foreach(i; 0 .. 2) { typeof(fn1()[i]) Common; writeln(Common[]); Common.delete or Common.Unset // Something similar like this } From, Vino.B static foreach does not create a new scope (if it did, it wouldn't work very well at module or class/struct scope). If you declare any variables inside a static foreach, give it an extra set of braces. - Jonathan m Davis Hi Jonathan, Sorry , not able to get you, can you please point our as to where we need to added the braces in the below example. void main () { Array!int Keycol; static foreach(i; 0 .. 3) { { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } } writeln (Datacol[], Keycol[]); } -Steve Hi Steve, if we add the braces we are getting the Error: undefined identifier Datacol void main () { Array!int Keycol; static foreach(i; 0 .. 3) { { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } } writeln (Datacol[], Keycol[]); } From, Vino.B
Re: Can I use D with the Microsoft universal windows platform?
On Wednesday, 3 January 2018 at 18:33:27 UTC, Ozan wrote: Hi Are there any use cases or libraries for using D in Microsoft's universal windows platform environment? It would be nice to have XBOX Apps build on D ;-) Regards Ozan I made a half functioning winrt wrapper once but eventually I couldn't get it to work anymore because of some inheritance mess with COM and it not being documented. I got a basic D GUI app to work with it if it uses XAML and it worked on hololens. https://github.com/WebFreak001/dwinrt
Re: Array is already defined
On Fri, Jan 05, 2018 at 03:56:30PM +, Vino via Digitalmars-d-learn wrote: [...] > Hi Jonathan, > > Sorry , not able to get you, can you please point our as to where we > need to added the braces in the below example. Try this: void main () { Array!int Keycol; static foreach(i; 0 .. 3) {{ typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k)); } }} writeln (Datacol[], Keycol[]); } T -- Tech-savvy: euphemism for nerdy.
Re: Array is already defined
On 1/5/18 10:56 AM, Vino wrote: On Friday, 5 January 2018 at 15:28:58 UTC, Jonathan M Davis wrote: On Friday, January 05, 2018 15:22:49 Vino via Digitalmars-d-learn wrote: Hi All, Request your help on how to unset or delete an array, in the below example, we get an error "Common is already defined". Auto fn1 () { Array!string Text; Array!string Number; return tuple(Text, Number); } Void main () { static foreach(i; 0 .. 2) { typeof(fn1()[i]) Common; writeln(Common[]); Common.delete or Common.Unset // Something similar like this } From, Vino.B static foreach does not create a new scope (if it did, it wouldn't work very well at module or class/struct scope). If you declare any variables inside a static foreach, give it an extra set of braces. - Jonathan m Davis Hi Jonathan, Sorry , not able to get you, can you please point our as to where we need to added the braces in the below example. void main () { Array!int Keycol; static foreach(i; 0 .. 3) { { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } } writeln (Datacol[], Keycol[]); } -Steve
Re: C++ Interop
see also https://github.com/Syniurge/Calypso/ although I'm having lots of issues using it on OSX On Fri, Jan 5, 2018 at 9:02 AM, qznc via Digitalmars-d-learn wrote: > I'm exploring [0] C++ interop after watching Walter's presentation [1]. > > I hit a block with classes as template parameters. This means vector > works, but vector does not. D seems to map vector!Foo to vector. > Likewise shared_ptr is a problem. Any way to fix that on the D side? > The ugly workaround is to adapt the C++ code. > > I understand that this mapping makes sense for function calls because > bar(Foo f) in D maps to bar(Foo *f) in C++. And C++ bar(Foo f) has no > equivalent in D because classes are reference types. > > On a related note, C++ interop requires to redeclare or even reimplement C++ > code. Has anybody started a libcpp-in-d project? I'm looking for basics like > vector and string. > > [0] https://github.com/qznc/d-cpptest > [1] https://youtu.be/IkwaV6k6BmM
Re: Array is already defined
On Friday, 5 January 2018 at 15:28:58 UTC, Jonathan M Davis wrote: On Friday, January 05, 2018 15:22:49 Vino via Digitalmars-d-learn wrote: Hi All, Request your help on how to unset or delete an array, in the below example, we get an error "Common is already defined". Auto fn1 () { Array!string Text; Array!string Number; return tuple(Text, Number); } Void main () { static foreach(i; 0 .. 2) { typeof(fn1()[i]) Common; writeln(Common[]); Common.delete or Common.Unset // Something similar like this } From, Vino.B static foreach does not create a new scope (if it did, it wouldn't work very well at module or class/struct scope). If you declare any variables inside a static foreach, give it an extra set of braces. - Jonathan m Davis Hi Jonathan, Sorry , not able to get you, can you please point our as to where we need to added the braces in the below example. void main () { Array!int Keycol; static foreach(i; 0 .. 3) { typeof(read()[i]) Datacol; Datacol.insertBack(sort(read[i].dup[]).uniq); foreach(k; read[i]) { Keycol.insertBack(Datacol[].countUntil(k));} } writeln (Datacol[], Keycol[]); } From, Vino.B
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: ~~ struct User { int id; string name; string email; } class ORM { } auto db = new ORM; auto users = db.select(User).where(email.like("*@hotmail.com")).limit(10); foreach(user; users) { writeln("user: " + user.name + "\n"); } ~~ this rust code is support it: https://github.com/diesel-rs/diesel/blob/master/examples/postgres/all_about_updates/src/lib.rs there's entity library exist in D Dub, which is an ORM framework, you may read the code for reference: https://code.dlang.org/packages/entity
why does unittest mangled name now contains full file path instead of fully qualified module name?
why does unittest mangled name now contains full file path instead of fully qualified module name? seems like a regression, previous behavior seemed better and not dependent on installation path.
Re: Array is already defined
On Friday, January 05, 2018 15:22:49 Vino via Digitalmars-d-learn wrote: > Hi All, > > Request your help on how to unset or delete an array, in the > below example, we get an error "Common is already defined". > > Auto fn1 () { > Array!string Text; > Array!string Number; > return tuple(Text, Number); > } > > Void main () { > static foreach(i; 0 .. 2) { > typeof(fn1()[i]) Common; > writeln(Common[]); > Common.delete or Common.Unset // Something similar like this > } > > From, > Vino.B static foreach does not create a new scope (if it did, it wouldn't work very well at module or class/struct scope). If you declare any variables inside a static foreach, give it an extra set of braces. - Jonathan m Davis
Array is already defined
Hi All, Request your help on how to unset or delete an array, in the below example, we get an error "Common is already defined". Auto fn1 () { Array!string Text; Array!string Number; return tuple(Text, Number); } Void main () { static foreach(i; 0 .. 2) { typeof(fn1()[i]) Common; writeln(Common[]); Common.delete or Common.Unset // Something similar like this } From, Vino.B
Re: Help optimizing UnCompress for gzipped files
On 1/5/18 1:01 AM, Christian Köstlin wrote: On 04.01.18 20:46, Steven Schveighoffer wrote: On 1/4/18 1:57 PM, Christian Köstlin wrote: Thanks Steve, this runs now faster, I will update the table. Still a bit irked that I can't match the C speed :/ But, I can't get your C speed to duplicate on my mac even with gcc, so I'm not sure where to start. I find it interesting that you are not using any optimization flags for gcc. I guess, the code in my program is small enough that the optimize flags do not matter... most of the stuff is pulled from libz? Which is dynamically linked against /usr/lib/libz.1.dylib. Yeah, I guess most of the bottlenecks are inside libz, or the memory allocator. There isn't much optimization to be done in the main program itself. I also cannot understand what I should do more (will try realloc with Mallocator) for the dlang-low-level variant to get to the c speed. D compiles just the same as C. So theoretically you should be able to get the same performance with a ported version of your C code. It's worth a shot. rust is doing quite well there I'll say a few words of caution here: 1. Almost all of these tests use the same C library to unzip. So it's really not a test of the performance of decompression, but the performance of memory management. And it appears that any test using malloc/realloc is in a different tier. Presumably because of the lack of copies (as discussed earlier). 2. Your rust test (I think, I'm not sure) is testing 2 things in the same run, which could potentially have dramatic consequences for the second test. For instance, it could already have all the required memory blocks ready, and the allocation strategy suddenly gets better. Or maybe there is some kind of caching of the input being done. I think you have a fairer test for the second option by running it in a separate program. I've never used rust, so I don't know what exactly your code is doing. 3. It's hard to make a decision based on such microbenchmarks as to which solution is "better" in an actual real-world program, especially when the state/usage of the memory allocator plays a huge role in this. -Steve
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 12:19:11 UTC, thedeemon wrote: On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: db.select(User).where(email.like("*@hotmail.com")).limit(10); You need to read about templates in D, here's a good guide: https://github.com/PhilippeSigaud/D-templates-tutorial Basically you can write a function like auto select(Class, string fieldName)(Class value) { ... and call it later as select!(User, "name")(u); Here User and "name" are compile-time arguments, you can pass there types, values and more exotic stuff like other templates. And inside the function you can use __traits(getMember, u, fieldName) to get field by its name from the passed value. See: https://dlang.org/spec/traits.html This was a really nice, short example btw. I'm not OP, but thanks for that.
Re: Consequences of casting away immutable from pointers
On Friday, 5 January 2018 at 04:10:54 UTC, Steven Schveighoffer wrote: Yes, this is undefined behavior. https://dlang.org/spec/const3.html#removing_with_cast The compiler assumes x is going to be 5 forever, so instead of loading the value at that address, it just loads 5 into a register (or maybe it just folds x == 5 into true). The compiler would likely be free to assume *p_x == 5 forever also, if it was clever enough. I'd recommend not doing this. -Steve I should have seen that. Thanks. That makes perfect sense.
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 13:09:25 UTC, Vino wrote: On Friday, 5 January 2018 at 12:47:39 UTC, thedeemon wrote: On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote: What exactly are you trying to do in Master()? Please find the full code, Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written. Hi, I am trying to implement data dictionary compression, and below is the function of the program, Function read: This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data. CSV file content: Miller America 23 JohnIndia 42 Baker Australia 21 Zsuwalski Japan 45 Baker America 45 Miller India 23 Function Main This function receives the data from the function read. Creates an array based of the function return type – ( typeof(read()[i]) Data ); Sorts the data and removes the duplicates and stores the data in the above array. Then using “countUntil” function we can accomplish the data dictionary compression. Result The above file will be stored as Data File: Data-Col1.txt which contains [Baker, John, Miller, Zsuwalski] Data-Col2.txt which contains [America, Australia , India, Japan] Data-Col3.txt which contains [21, 23, 42, 45] Index File: Index-Col1.txt which contains [2, 1, 0, 3, 0, 2] Index -Col2.txt which contains [0, 2, 1, 3, 0, 2] Index -Col3.txt which contains [1, 2, 0, 3, 3, 1] The program works for a single column. From, Vino.B More Info: If we change the below line static foreach(i; 0 .. 1) Output: ["Baker", "John", "Miller", "Zsuwalski"][2, 1, 0, 3, 0, 2] static foreach(i; 1 .. 2) ["America", "Austrilia", "India", "Japan"][0, 2, 1, 3, 0, 2]) static foreach(i; 2 .. 3) [21, 23, 42, 45][1, 2, 0, 3, 3, 1] Instead of manually chaning the values I used the variable Size where the value of the Size if from the read function (read[3] ) where read[3] is rSize = record.length; If I use the variable Size as static foreach(i; 0 .. Size) I am getting an error : “Error: variable Size cannot be read at compile time”. From, Vino.B
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 12:47:39 UTC, thedeemon wrote: On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote: What exactly are you trying to do in Master()? Please find the full code, Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written. Hi, I am trying to implement data dictionary compression, and below is the function of the program, Function read: This function read a csv file which contains 3 column as and stores the value of each column in an array Col1: Array1 (Ucol1), Col2: Array2 (Ucol2), Col3: Array3(Ucol3) and returns the data. CSV file content: Miller America 23 JohnIndia 42 Baker Australia 21 Zsuwalski Japan 45 Baker America 45 Miller India 23 Function Main This function receives the data from the function read. Creates an array based of the function return type – ( typeof(read()[i]) Data ); Sorts the data and removes the duplicates and stores the data in the above array. Then using “countUntil” function we can accomplish the data dictionary compression. Result The above file will be stored as Data File: Data-Col1.txt which contains [Baker, John, Miller, Zsuwalski] Data-Col2.txt which contains [America, Australia , India, Japan] Data-Col3.txt which contains [21, 23, 42, 45] Index File: Index-Col1.txt which contains [2, 1, 0, 3, 0, 2] Index -Col2.txt which contains [0, 2, 1, 3, 0, 2] Index -Col3.txt which contains [1, 2, 0, 3, 3, 1] The program works for a single column. From, Vino.B
C++ Interop
I'm exploring [0] C++ interop after watching Walter's presentation [1]. I hit a block with classes as template parameters. This means vector works, but vector does not. D seems to map vector!Foo to vector. Likewise shared_ptr is a problem. Any way to fix that on the D side? The ugly workaround is to adapt the C++ code. I understand that this mapping makes sense for function calls because bar(Foo f) in D maps to bar(Foo *f) in C++. And C++ bar(Foo f) has no equivalent in D because classes are reference types. On a related note, C++ interop requires to redeclare or even reimplement C++ code. Has anybody started a libcpp-in-d project? I'm looking for basics like vector and string. [0] https://github.com/qznc/d-cpptest [1] https://youtu.be/IkwaV6k6BmM
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 12:40:41 UTC, Vino wrote: What exactly are you trying to do in Master()? Please find the full code, Sorry, I'm asking what problem are you solving, what the program should do, what is its idea. Not what code you have written.
Re: Dub and libraries
On Thursday, 4 January 2018 at 19:05:59 UTC, Russel Winder wrote: Is it the case that, for library things on the Dub repository, Dub will only create library archives, .a, that it is unable to create shared objects and DLLs? If they have "targetType" set to "dynamicLibrary" dub creates shared libraries. https://code.dlang.org/package-format?lang=sdl#target-types
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 12:10:33 UTC, thedeemon wrote: On Friday, 5 January 2018 at 09:09:00 UTC, Vino wrote: Thank you very much, can you suggest the best way around this issue. What exactly are you trying to do in Master()? The code seems very broken. Each time you write read[i] is will call read() and read the whole file, you're going to read the file so many times in this code. I don't think that was the intent. Hi, Please find the full code, the below code will read a ColRead.csv file which contains the below entry Miller America 23 JohnIndia 42 Baker Austrilia 21 Zsuwalski Japan 45 Baker America 45 Miller India 23 import std.algorithm: countUntil, joiner, sort, uniq; import std.container.array; import std.csv: csvReader; import std.stdio: File, writeln; import std.typecons: Tuple, tuple; auto read (){ Array!string Ucol1, Ucol2; Array!int Ucol3; int rSize; auto file = File("C:\\Users\\bheev1\\Desktop\\Current\\Script\\Others\\ColRead.csv", "r"); foreach (record; file.byLineCopy.joiner("\n").csvReader!(Tuple!(string, string, int))) { Ucol1.insertBack(record[0]); Ucol2.insertBack(record[1]); Ucol3.insertBack(record[2]); rSize = record.length; } return tuple(Ucol1, Ucol2, Ucol3, rSize); } void main () { Array!int Key; int Size = read[3]; static foreach(i; 0 .. Size) { typeof(read()[i]) Data; Data.insertBack(sort(read[0].dup[]).uniq); foreach(i; read[i]) { Key.insertBack(Data[].countUntil(i)); } } }
Re: weird exception on windows
On Monday, 18 December 2017 at 22:49:30 UTC, unleashy wrote: On Friday, 15 December 2017 at 21:56:48 UTC, Steven Schveighoffer wrote: On 12/15/17 10:08 AM, Kagamin wrote: Maybe this https://issues.dlang.org/show_bug.cgi?id=18084 Thanks for looking into this. I created a PR to fix. Szabo, can you please try with this patch and see if it fixes your issue? https://github.com/dlang/phobos/pull/5932 -Steve I created the original issue in Szabo's post. I applied your fix, but nothing changed—the test program still crashes with the same exception :/ What gives? It looks like this fix does not work: https://github.com/dlang/phobos/pull/5932 Does anyone know how to debug this crash?
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: db.select(User).where(email.like("*@hotmail.com")).limit(10); You need to read about templates in D, here's a good guide: https://github.com/PhilippeSigaud/D-templates-tutorial Basically you can write a function like auto select(Class, string fieldName)(Class value) { ... and call it later as select!(User, "name")(u); Here User and "name" are compile-time arguments, you can pass there types, values and more exotic stuff like other templates. And inside the function you can use __traits(getMember, u, fieldName) to get field by its name from the passed value. See: https://dlang.org/spec/traits.html
Re: Error: variable i cannot be read at compile time
On Friday, 5 January 2018 at 09:09:00 UTC, Vino wrote: Thank you very much, can you suggest the best way around this issue. What exactly are you trying to do in Master()? The code seems very broken. Each time you write read[i] is will call read() and read the whole file, you're going to read the file so many times in this code. I don't think that was the intent.
Re: Error: variable i cannot be read at compile time
On Thursday, 4 January 2018 at 18:49:21 UTC, Ali Çehreli wrote: On 01/04/2018 08:51 AM, Vino wrote: > auto read () { [...] > return tuple(Ucol1, Ucol2, Ucol3, rSize); > } read() returns a tuple of values of different types. > for(int i = 0; i < Size; i++) { > typeof(read()[i]) Datacol; typeof is a compile-time expression but there cannot be a consistent result to that expression when i is not known at compile-time. You might try using a 'static foreach' but this time Size is not a compile-time expression: static foreach(i; 0 .. Size) { typeof(read()[i]) Datacol; Error: variable Size cannot be read at compile time Ali Hi Ali, Thank you very much, can you suggest the best way around this issue. From, Vino.B
Re: What could this OptLink Error mean?
On Thursday, 4 January 2018 at 21:33:58 UTC, Dukc wrote: On Saturday, 30 December 2017 at 00:49:48 UTC, user1234 wrote: The deps have to be rebuild too. After downloading dmd 78, it started to work. It's likely you were right about the issue, DUB rebuilt everything after detecting new compiler version. Sorry for my late reply. From a compiler version to another, the ABI can change (examples are: mangling, new overloads as seen with format a couple of months ago). You compiled the program with a slightly different ABI than the one used to compile the deps as static libraries. I don't know why DUB didn't detect this but as said user1234 always try " --force" on DUB or rebuild deps manually to eliminate this possible cause of problems.
Re: How do i activate the latest nightly without knowing the date ?
On Thursday, 4 January 2018 at 22:07:39 UTC, user789 wrote: On Thursday, 4 January 2018 at 20:37:16 UTC, user789 wrote: On Thursday, 4 January 2018 at 20:28:47 UTC, user789 wrote: After using the script to setup DMD nightly i read in the output that i have to run "source ~/dlang/dmd-master-2018-01-04/activate" to have DMD DUB etc working. Is there another activation command, working without the date, suitable for a cron job ? "source ~/dlang/dmd-nightly/activate" doesn't work but would be better when you just want to use the nightly F1nally i found that just --- wget https://nightlies.dlang.org/dmd-nightly/dmd.master.linux.tar.xz tar xf ./dmd.master.linux.tar.xz03:12 export PATH=$PATH:`pwd`/dmd2/linux/bin64 --- works fine. maybe you should add something to the script to install and activate latest nightly in one step. Oh some unrelated log material found its little guy way in the copy/paste --- https://nightlies.dlang.org/dmd-nightly/dmd.master.linux.tar.xz tar xf ./dmd.master.linux.tar.xz export PATH=$PATH:`pwd`/dmd2/linux/bin64 ---
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 08:34:00 UTC, user789 wrote: On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: ~~ struct User { int id; string name; string email; } class ORM { } auto db = new ORM; auto users = db.select(User).where(email.like("*@hotmail.com")).limit(10); foreach(user; users) { writeln("user: " + user.name + "\n"); } ~~ this rust code is support it: https://github.com/diesel-rs/diesel/blob/master/examples/postgres/all_about_updates/src/lib.rs Well in D this would be more something like that: auto users = db.select!(User).map!(a => a.email.like("*@hotmail.com")).take(10); Oh, I realize that your Q was more: "which package should i use to write this way ?"
Re: I want to transmit the class name and the member name in the method
On Friday, 5 January 2018 at 07:40:14 UTC, Brian wrote: I think code style like: ~~ struct User { int id; string name; string email; } class ORM { } auto db = new ORM; auto users = db.select(User).where(email.like("*@hotmail.com")).limit(10); foreach(user; users) { writeln("user: " + user.name + "\n"); } ~~ this rust code is support it: https://github.com/diesel-rs/diesel/blob/master/examples/postgres/all_about_updates/src/lib.rs Well in D this would be more something like that: auto users = db.select!(User).map!(a => a.email.like("*@hotmail.com")).take(10);