Re: Mallocator and 'shared'
On Tuesday, 14 February 2017 at 15:57:47 UTC, Moritz Maxeiner wrote: You seem to be trying to argue against someone stating memory barriers should be emitted automatically, though I don't know why you think that's me; You initially stated that Memory barriers are a bad idea because they don't defend from a race condition, but they look like they do You were concerned that implementation doesn't match FAQ regarding automatic barriers. I thought we were talking about that.
Re: Mallocator and 'shared'
On Wednesday, 15 February 2017 at 09:49:52 UTC, Kagamin wrote: On Tuesday, 14 February 2017 at 15:57:47 UTC, Moritz Maxeiner wrote: You seem to be trying to argue against someone stating memory barriers should be emitted automatically, though I don't know why you think that's me; You initially stated that Memory barriers are a bad idea because they don't defend from a race condition, but they look like they do You were concerned that implementation doesn't match FAQ regarding automatic barriers. I thought we were talking about that. The implementation does match the FAQ, since the FAQ states they aren't implemented. It's whether or not the FAQ states that they are supposed to be implemented that's the issue in my opinion. Also, what I initially replied to you about regarding memory barriers - not automatic emission of memory barriers - was your statement that they are a bad idea.
Better than "Clock.currStdTime()/10000000"
I need to measure time elapsed in seconds, like this: auto start = Clock.currStdTime(); // some stuff auto stop = Clock.currStdTime(); auto duration = (stop-start)/1000; This works, but I wonder if there is something better that using the magic constant 1000. I read about 10.secs giving the duration of 10 seconds, but I don't understand how to adapt this to my case. I've read the documentation of core.time, std.datetime (and the Introduction to this package) but I can't make head or tail of it. PS: It's not about benchmarking. I'd like to show the user the time elapsed.
Re: Better than "Clock.currStdTime()/10000000"
15.02.2017 16:19, berni пишет: I need to measure time elapsed in seconds, like this: auto start = Clock.currStdTime(); // some stuff auto stop = Clock.currStdTime(); auto duration = (stop-start)/1000; This works, but I wonder if there is something better that using the magic constant 1000. I read about 10.secs giving the duration of 10 seconds, but I don't understand how to adapt this to my case. I've read the documentation of core.time, std.datetime (and the Introduction to this package) but I can't make head or tail of it. PS: It's not about benchmarking. I'd like to show the user the time elapsed. doesn't it work for you? ``` void main() { import std.datetime, core.thread, std.stdio; MonoTime before = MonoTime.currTime; Thread.sleep(dur!"msecs"(1000)); MonoTime after = MonoTime.currTime; Duration timeElapsed = after - before; writeln(timeElapsed); } ``` I get: "1 sec, 26 μs, and 4 hnsecs"
Re: Better than "Clock.currStdTime()/10000000"
On Wednesday, 15 February 2017 at 13:19:57 UTC, berni wrote: I need to measure time elapsed in seconds, like this: auto start = Clock.currStdTime(); // some stuff auto stop = Clock.currStdTime(); auto duration = (stop-start)/1000; This works, but I wonder if there is something better that using the magic constant 1000. I read about 10.secs giving the duration of 10 seconds, but I don't understand how to adapt this to my case. I've read the documentation of core.time, std.datetime (and the Introduction to this package) but I can't make head or tail of it. PS: It's not about benchmarking. I'd like to show the user the time elapsed. How about something like this: import std.stdio; void main() { import core.thread, core.time; import std.conv, std.datetime; auto start = Clock.currTime; Thread.sleep(500.msecs); auto diff = Clock.currTime - start; diff.writeln; // Duration (diff.total!"msecs").writeln; (to!("msecs", long) (diff.to!TickDuration)).writeln; (to!("msecs", double)(diff. to!TickDuration)).writeln; } Note that TickDuration is deprecated.
Re: Better than "Clock.currStdTime()/10000000"
On Wednesday, February 15, 2017 16:27:34 drug via Digitalmars-d-learn wrote: > 15.02.2017 16:19, berni пишет: > > I need to measure time elapsed in seconds, like this: > >> auto start = Clock.currStdTime(); > >> // some stuff > >> auto stop = Clock.currStdTime(); > >> auto duration = (stop-start)/1000; > > > > This works, but I wonder if there is something better that using the > > magic constant 1000. I read about 10.secs giving the duration of 10 > > seconds, but I don't understand how to adapt this to my case. I've read > > the documentation of core.time, std.datetime (and the Introduction to > > this package) but I can't make head or tail of it. > > > > PS: It's not about benchmarking. I'd like to show the user the time > > elapsed. > > doesn't it work for you? > ``` > void main() > { > import std.datetime, core.thread, std.stdio; > > MonoTime before = MonoTime.currTime; > Thread.sleep(dur!"msecs"(1000)); > MonoTime after = MonoTime.currTime; > Duration timeElapsed = after - before; > > writeln(timeElapsed); > } > ``` > I get: "1 sec, 26 μs, and 4 hnsecs" This is the correct way to do it. Using SysTime for timing things is wrong, because it's not a monotonic clock. So, it could be changed - even going backwards - while you're doing the timing. MonoTime, on the other hand, uses the system's monotonic clock and is therefore guaranteed to move forward at a fixed rate. - Jonathan M Davis
A bug?
I'm not sure if this is considered a bug: import std.stdio; import std.string; int c = 0; void main() { try { write(++c," "); stdout.flush(); int[10] tmp; throw new Exception(format("%s",tmp)); } finally { main(); } } Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Segmentation fault
Re: A bug?
15.02.2017 19:00, berni пишет: I'm not sure if this is considered a bug: import std.stdio; import std.string; int c = 0; void main() { try { write(++c," "); stdout.flush(); int[10] tmp; throw new Exception(format("%s",tmp)); } finally { main(); } } Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Segmentation fault No, you recursively call main() and get segfault (due to stack overflow) as expected If you downsize tmp array then you get segfault later
Getting a segfault here, why?
I'm getting a segmentation fault in vibe.d web interface class. Does referring "this" in an "if" or "switch" within a method cause segfault? Trying to find it but working with a debugger in D is not straight forward. Not yo talk of interpretating the debugger output. How has things improved since this thread (http://forum.dlang.org/post/1339326434.14823.4.camel@localhost)?
Re: Getting a segfault here, why?
On Wednesday, 15 February 2017 at 18:19:18 UTC, aberba wrote: Trying to find it but working with a debugger in D is not straight forward. Not yo talk of interpretating the debugger output. On linux it is pretty easy. Just compile with `-g` to dmd and run the program in gdb. Run till it crashes and it should tell you the file and line of where.
Re: A bug?
On Wednesday, 15 February 2017 at 16:11:36 UTC, drug wrote: No, you recursively call main() and get segfault (due to stack overflow) as expected I thought, that an stack overflow leeds to an exception. But that's not true, as I now see. Thanks for your answer.
Re: Better than "Clock.currStdTime()/10000000"
On Wednesday, 15 February 2017 at 15:58:41 UTC, Jonathan M Davis wrote: [...] MonoTime before = MonoTime.currTime; Thread.sleep(dur!"msecs"(1000)); MonoTime after = MonoTime.currTime; Duration timeElapsed = after - before; writeln(timeElapsed); } ``` I get: "1 sec, 26 μs, and 4 hnsecs" This is the correct way to do it. [...] Oh, thanks for noting this. As I finally needed the seconds as an int (or long) I had to use timeElapsed.total!"seconds" which I would not have found out without Seb's posting. :-)
Get the address of an object, within the object itself
Hi all, sorry if this question is silly, but is it possible to get the address of an object within the object itself? e.g. class Node { this() { writeln(&this); // Doesn't work } } auto node = new Node(); writeln(&node); // Does work Thanks very much, Cheers, Andrew.
Re: Get the address of an object, within the object itself
On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- learn wrote: > Hi all, sorry if this question is silly, but is it possible to > get the address of an object within the object itself? > > e.g. > > class Node > { > this() > { > writeln(&this); // Doesn't work > } > } > > auto node = new Node(); > writeln(&node); // Does work This does _not_ give you the address of the Node object. It gives you the address of the reference. - Jonathan M Davis
Re: Get the address of an object, within the object itself
On Wednesday, February 15, 2017 13:33:23 Jonathan M Davis via Digitalmars-d- learn wrote: > On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- > learn wrote: > > Hi all, sorry if this question is silly, but is it possible to > > get the address of an object within the object itself? > > > > e.g. > > > > class Node > > { > > > > this() > > { > > > > writeln(&this); // Doesn't work > > > > } > > > > } > > > > auto node = new Node(); > > writeln(&node); // Does work > > This does _not_ give you the address of the Node object. It gives you the > address of the reference. IIRC, the only way to get the address of the object itself would be to cast it to void*, but it's not something that I do normally, so I'd have to experiment a bit to be sure. - Jonathan M Davis
Re: Get the address of an object, within the object itself
On Wednesday, 15 February 2017 at 21:37:12 UTC, Jonathan M Davis wrote: On Wednesday, February 15, 2017 13:33:23 Jonathan M Davis via Digitalmars-d- learn wrote: On Wednesday, February 15, 2017 21:27:00 Andrew Chapman via Digitalmars-d- learn wrote: > Hi all, sorry if this question is silly, but is it possible > to get the address of an object within the object itself? > > e.g. > > class Node > { > > this() > { > > writeln(&this); // Doesn't work > > } > > } > > auto node = new Node(); > writeln(&node); // Does work This does _not_ give you the address of the Node object. It gives you the address of the reference. IIRC, the only way to get the address of the object itself would be to cast it to void*, but it's not something that I do normally, so I'd have to experiment a bit to be sure. - Jonathan M Davis Thanks Jonathan. Good point about the reference address. I can work around this quite easily, but I was curious. I will try the void* cast and see what happens. Cheers.
Convert call to a string
I'd like to convert a call to a string for debug printing purposes for example: ``` import std.stdio : writeln; void someFunction(int x, string y){} string myCall = debugPrint(someFunction(1, "hello")); writeln(myCall); ``` writes: someFunction(1, "hello") Does this functionality exists? If not how can I construct it? Please note that the call `someFunction(1, "hello")` should also be executed. Thank you
Re: Convert call to a string
On Wed, Feb 15, 2017 at 10:07:22PM +, data pulverizer via Digitalmars-d-learn wrote: > I'd like to convert a call to a string for debug printing purposes for > example: > > > ``` > import std.stdio : writeln; > void someFunction(int x, string y){} > string myCall = debugPrint(someFunction(1, "hello")); > writeln(myCall); > ``` > writes: > someFunction(1, "hello") > > > Does this functionality exists? If not how can I construct it? Please > note that the call `someFunction(1, "hello")` should also be executed. [...] Try this: auto debugPrint(string expr)() { writeln(expr); return mixin(expr); } string myCall = debugPrint!`someFunction(1, "hello")`; T -- Klein bottle for rent ... inquire within. -- Stephen Mulraney
Re: Convert call to a string
On Wed, Feb 15, 2017 at 02:18:48PM -0800, H. S. Teoh via Digitalmars-d-learn wrote: [...] > Try this: > > auto debugPrint(string expr)() { > writeln(expr); > return mixin(expr); > } > > string myCall = debugPrint!`someFunction(1, "hello")`; [...] OTOH, that won't work with local variables (it'd only print the variable names, not the values). Presumably you'd also want to print the variables too. So perhaps something like this instead: auto debugPrint(alias fun, A...)(A args) { writefln("%s(%(%s, %))", __traits(identifier, fun), [args]); return fun(args); } string arg = "hello"; string myCall = debugPrint!someFunction(1, arg); T -- I see that you JS got Bach.
Re: Convert call to a string
On Wednesday, 15 February 2017 at 22:34:22 UTC, H. S. Teoh wrote: auto debugPrint(alias fun, A...)(A args) { writefln("%s(%(%s, %))", __traits(identifier, fun), [args]); return fun(args); } string arg = "hello"; string myCall = debugPrint!someFunction(1, arg); `[args]` doesn't work when the tuple elements don't have a common type. But you can pass `args` as is to writefln and generate the format string accordingly: import std.range: repeat; import std.string: join; immutable string argsfmt = "%s".repeat(args.length).join(", "); writefln("%s(" ~ argsfmt ~ ")", __traits(identifier, fun), args);
Re: Convert call to a string
On Wed, Feb 15, 2017 at 10:58:42PM +, ag0aep6g via Digitalmars-d-learn wrote: > On Wednesday, 15 February 2017 at 22:34:22 UTC, H. S. Teoh wrote: > > auto debugPrint(alias fun, A...)(A args) { > > writefln("%s(%(%s, %))", __traits(identifier, fun), [args]); > > return fun(args); > > } > > > > string arg = "hello"; > > string myCall = debugPrint!someFunction(1, arg); > > `[args]` doesn't work when the tuple elements don't have a common > type. Very good point. > But you can pass `args` as is to writefln and generate the format > string accordingly: > > > import std.range: repeat; > import std.string: join; > > immutable string argsfmt = "%s".repeat(args.length).join(", "); > writefln("%s(" ~ argsfmt ~ ")", __traits(identifier, fun), args); > Excellent idea! T -- Life is complex. It consists of real and imaginary parts. -- YHL
User imput string int and float[DOUBT]
How do I make a class person where I use set and get methods to imput the user type: Import std.stdio; class person { private: string name, address; int age; float height; public: void setNome() { write("Enter Your Name:"); // the problem is here how am I going to read the imput of a string typed by the user? } void setIty() { write("Enter Your Age:"); // Another problem here also to read integer values like I would? } void setHeight() { write("Enter Your Height:"); // Another problem here also to read floats or double values like I would? } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setName(); p.setIdade(); p.setHeight(); p.getName(); p.getIdade(); p.getHeight(); }
Re: Convert call to a string
On Wednesday, 15 February 2017 at 22:07:22 UTC, data pulverizer wrote: That's great, thanks both of you!
Re: User imput string int and float[DOUBT]
On 02/15/2017 03:20 PM, Jean Cesar wrote: How do I make a class person where I use set and get methods to imput the user type: I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali
Declaring constant references in struct members
Hi, Say I have a struct S that holds a reference to an object O. Is there a way to express that I want to be able to change the reference, but not what the reference points to? Thanks. struct S { O object; } class O { size_t things. }
Re: Declaring constant references in struct members
On Thursday, 16 February 2017 at 00:43:30 UTC, David Zhang wrote: struct S { O object; } import std.typecons; Rebindable!O object; http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html
Re: Declaring constant references in struct members
On Thu, Feb 16, 2017 at 12:43:30AM +, David Zhang via Digitalmars-d-learn wrote: > Hi, > > Say I have a struct S that holds a reference to an object O. Is there > a way to express that I want to be able to change the reference, but > not what the reference points to? Thanks. > > struct S { > O object; > } > > class O { > size_t things. > } Maybe have a look at std.typecons.Rebindable? T -- What do you call optometrist jokes? Vitreous humor.
Re: Declaring constant references in struct members
On Thursday, 16 February 2017 at 00:49:45 UTC, Adam D. Ruppe wrote: On Thursday, 16 February 2017 at 00:43:30 UTC, David Zhang wrote: struct S { O object; } import std.typecons; Rebindable!O object; http://dpldocs.info/experimental-docs/std.typecons.Rebindable.html Is there a similar mechanism for one struct holding another? Otherwise, you get a cannot modify X with immutable members error. eg: struct A { B b; } struct B { const size_t something; } A a = A(B(16)); //attempt to replace a.b with new B a.b = B(32); //error: cannot modify struct a.b B with immutable members
Re: Declaring constant references in struct members
On Thursday, 16 February 2017 at 01:05:58 UTC, David Zhang wrote: Is there a similar mechanism for one struct holding another? You'd have to make the member a pointer to the struct. immutable(B)* b;
Re: User imput string int and float[DOUBT]
On Wednesday, 15 February 2017 at 23:40:41 UTC, Ali Çehreli wrote: On 02/15/2017 03:20 PM, Jean Cesar wrote: How do I make a class person where I use set and get methods to imput the user type: I have some information here: http://ddili.org/ders/d.en/input.html You should also know how to read strings: http://ddili.org/ders/d.en/strings.html And this section about refactoring has the concept of a readInt() function template: http://ddili.org/ders/d.en/functions.html#ix_functions.refactor Combining all three: import std.stdio; import std.traits; auto read(T)(ref T t, string message) if (!isSomeString!T) { writef("%s: ", message); readf(" %s", &t); return t; } auto read(S)(ref S s, string message) if (isSomeString!S) { import std.string : strip; writef("%s: ", message); s = readln().strip(); return s; } class person { private: string name, address; int age; float height; public: void setNome() { read(name, "Enter Your Name"); } void setIty() { read(age, "Enter Your Age"); } void setHeight() { read(height, "Enter Your Height"); } float getHeight() { return height; } int getIty() { return age; } string getNome() { return name; } } void main () { person p = new person(); p.setNome(); p.setIty(); p.setHeight(); writeln(p.getNome()); writeln(p.getIty()); writeln(p.getHeight()); } Unrelated, a bunch of get/set methods is commonly seen as inferior to a design where another piece of code does the reading and makes the object after the fact: person readPerson(File input) { // ... parse the input ... // Potentially, use the constructor: auto p = new person(name, age, /* ... */); return p; } One reason is the fact that the person may be seen as incomplete and unusable unless all fields are set. Again, it's beside the point... :) Ali So I'm a beginner in this language and have very little time I started I'm interested in apprehending concepts of object orientation polymorphism inheritance, multiple inheritance as in c ++, but I did not understand how to use constructor in it Because I simply did. Class person { person(){} ~ Person () {} } And error ...
Re: User imput string int and float[DOUBT]
On 02/15/2017 05:49 PM, Jean Cesar wrote: > So I'm a beginner in this language and have very little time I started > I'm interested in apprehending concepts of object orientation > polymorphism inheritance, multiple inheritance as in c ++ D is similar to C++ but also very different. > but I did not > understand how to use constructor in it > Because I simply did. > > Class person > { >person(){} >~ Person () {} > } > > And error ... In D, constructor is always called this(): class Person { this(){} ~this() {} } void main() { auto p = new Person(); } Ali
opApply with Type Inference and Templates?
I am trying to do opApply to work when the delegate passed when it is and isn't nogc/nothrow. As soon as you involve a template though, type inference goes out the door. I want to be able to use opApply with templates (to get the auto @nogc/nothrow deducation passed on the delegate passed) but still be able to use type inference. Is there any way to do this?
Re: opApply with Type Inference and Templates?
On Thursday, 16 February 2017 at 03:20:12 UTC, Jerry wrote: I am trying to do opApply to work when the delegate passed when it is and isn't nogc/nothrow. As soon as you involve a template though, type inference goes out the door. I want to be able to use opApply with templates (to get the auto @nogc/nothrow deducation passed on the delegate passed) but still be able to use type inference. Is there any way to do this? No, by any chance do you ask this for the tuple unpacking PR ? If so I've also tried and failed.