running a command in a directory using std.process
As far as I can tell std.process can only run commands in the working directory of the currently executing function. I want to execute a certain program inside a subdirectory on windows and can't get it to work: myproject |- subdir So my executable has myproject as working directory. And I want to execute dmd with subdir as working directory. -- Kind Regards Benjamin Thaut
Copying to an immutable array in a costructor
This shows a limitation of the D type system: import std.algorithm: copy; immutable int[2] data; static this() { foreach (i, x; [10, 20]) data[i] = x; // OK data[] = [10, 20]; // OK [10, 20].copy(data[]); // Error. } void main() {} Bye, bearophile
Re: running a command in a directory using std.process
On Thursday, 24 October 2013 at 06:25:40 UTC, Benjamin Thaut wrote: As far as I can tell std.process can only run commands in the working directory of the currently executing function. I want to execute a certain program inside a subdirectory on windows and can't get it to work: myproject |- subdir So my executable has myproject as working directory. And I want to execute dmd with subdir as working directory. Isn't it possible to execute a command like cd subdir dmd?
Re: Copying to an immutable array in a costructor
On Thursday, October 24, 2013 09:02:24 bearophile wrote: This shows a limitation of the D type system: import std.algorithm: copy; immutable int[2] data; static this() { foreach (i, x; [10, 20]) data[i] = x; // OK data[] = [10, 20]; // OK [10, 20].copy(data[]); // Error. } void main() {} It's a compiler bug. immutable data should not be initialized more than once. - Jonathan M Davis
A question about std.uri
Is there a reason why the decode/encode functions and such throw Errors ? Seems like an error (Non-recoverable) is a bit harsh, why isn't the Exception class used ? Kind regards, Danny Arends http://www.dannyarends.nl
Static if/else blocks and ddoc
Hello all, An interesting observation I made today: if you have a static if/else block in your code, then ddoc will ignore anything inside the else block. I've filed this as a bug report: http://d.puremagic.com/issues/show_bug.cgi?id=11337 ... but I wonder if anyone can give insight into why this should be so? Thanks best wishes, -- Joe
Re: A question about std.uri
On Thursday, 24 October 2013 at 08:35:15 UTC, Danny Arends wrote: Is there a reason why the decode/encode functions and such throw Errors ? Seems like an error (Non-recoverable) is a bit harsh, why isn't the Exception class used ? Kind regards, Danny Arends http://www.dannyarends.nl Some people on the IRC channel said it might be a bug: http://d.puremagic.com/issues/show_bug.cgi?id=11338 Gr, Danny Arends
Re: A question about std.uri
On Thursday, 24 October 2013 at 09:46:52 UTC, Danny Arends wrote: Some people on the IRC channel said it might be a bug: http://d.puremagic.com/issues/show_bug.cgi?id=11338 Gr, Danny Arends And a pull request: https://github.com/D-Programming-Language/phobos/pull/1659 Gr, Danny Arends http://www.dannyarends.nl
Re: Copying to an immutable array in a costructor
Jonathan M Davis: It's a compiler bug. immutable data should not be initialized more than once. My point was another one, regarding this refused code: import std.algorithm: copy; immutable int[2] data; static this() { [10, 20].copy(data[]); // Error. } void main() {} Bye, bearophile
Re: matrix business in D
Thank you very much for your help! I think I start to understand it better. On Wednesday, 23 October 2013 at 14:48:52 UTC, John Colvin wrote: On Wednesday, 23 October 2013 at 14:00:46 UTC, Yura wrote: Dear all, Thank you for your replies! Regarding Julia - it seems to be interesting, but - it is too fresh, and from what I understood, it is not compiled. I think D language would be more interesting for me and suitable for my needs (scientific computing). Yes, numpy/scipy is OK, but since I have now some time I would like to learn one compiled language which is more close to the hardware, I have done some linear algebra in D. If you are comfortable calling C functions, you can easily call into existing solutions, because it is trivial to call into C from D. This is very interesting since as you know lots of code is written in c. GSL is a good example. The only problem is how to use it. The thing is that i don't know c, but the question is whether I really need to be skilled in c to be able to call c functions. My gut feeling is that no, I don't need to be skilled. I have installed gsl on my computer. But what I need is a good example of a code/codes on how to call this library from d programming language. E.g. I have tried to use gsl. I have written a code in c (simple.c): --- #include stdio.h #include gsl/gsl_sf_bessel.h double fun(double x) //main (void) { // x = 5.0; double y = gsl_sf_bessel_J0 (x); // printf (J0(%g) = %.18e\n, x, y); return y; } Also, I have written a di file (simple.di): extern (C): double fun(double); And finally, d code (simple.d): -- import std.stdio, std.string, std.array; import std.conv; import std.stdio; import simple; void main(){ writeln( fun(10.0) ); } Unfortunately, when I compile it it says: dmd simple.d simple.o simple.d(8): Error: undefined identifier fun Could one provide a working clear example how to use gsl in D? I have tried SciD and it apparently works, though I did not test it so far. I think a tutorial on how to use D in scientific programming would be very appreciated and could attract more people to D. PS Thank all of you for helping. Don't call everything the same name. At the very least don't have the di and d file with the same name. Once you've done that, it will compile but the linker will start to complain. You will need to link to the gsl and gslcblas libraries, making your compilation command this: dmd test.d simple.o -L-lgsl -L-lgslcblas The simplest possible example of using gsl would be this: simpleGSL.d import std.stdio; extern(C) double gsl_sf_bessel_J0(double); void main() { writeln(gsl_sf__bessel_J0(10)); } compile with dmd -L-lgsl -L-lgslcblas simpleGSL.d If you were doing this seriously you would want to create a load of d or di files containing the extern(C) declarations for all the different gsl things you need. Also, you might want to take a look at dstep: https://github.com/jacob-carlborg/dstep which might be able to auto-generate them all for you.
Re: Copying to an immutable array in a costructor
On Thursday, 24 October 2013 at 10:58:30 UTC, bearophile wrote: Jonathan M Davis: It's a compiler bug. immutable data should not be initialized more than once. My point was another one, regarding this refused code: import std.algorithm: copy; immutable int[2] data; static this() { [10, 20].copy(data[]); // Error. } void main() {} Bye, bearophile Reduced: immutable int[] data; void foo(int[] data) {} static this() { foo(data); }
Re: Copying to an immutable array in a costructor
On Thursday, 24 October 2013 at 07:02:25 UTC, bearophile wrote: This shows a limitation of the D type system: import std.algorithm: copy; immutable int[2] data; static this() { foreach (i, x; [10, 20]) data[i] = x; // OK data[] = [10, 20]; // OK [10, 20].copy(data[]); // Error. } void main() {} Bye, bearophile Because control flow check for immutable data is almost absent. I don't know any situation where are such checks (except constructors). There is similar issue with respect to initializsing immutable member field in struct constructor. There is issue in bugzilla filed for it (it looks like it will be fixed), where Andrei proposed a concept of cooked object, basically implying control flow for initializaing immutable members. I think that the code you posted as an additional case for the issue.
Re: GhostDoc for VisualD?
On Wednesday, 23 October 2013 at 15:56:33 UTC, Namespace wrote: Is there anything like this for VisualD? As far as I understand the GhostDoc website it generates prose comments from the type information? The only reason I can think of are weird enterprise requirements like every method must have at least 5 lines of documentation. Maybe you want some specific part of GhostDoc, which is actually useful?
Re: GhostDoc for VisualD?
On Thursday, 24 October 2013 at 11:59:37 UTC, qznc wrote: On Wednesday, 23 October 2013 at 15:56:33 UTC, Namespace wrote: Is there anything like this for VisualD? As far as I understand the GhostDoc website it generates prose comments from the type information? The only reason I can think of are weird enterprise requirements like every method must have at least 5 lines of documentation. Maybe you want some specific part of GhostDoc, which is actually useful? I want to auto generate comments. But GhostDoc is not compatible with VisualD. See: http://community.submain.com/forums/thread/3461.aspx
Bug in RefCounted?
I'm writing a D wrapper for a C library. I was planning to use RefCounted structs to control the lifetime of objects created by this library. Please check the following example: http://dpaste.dzfl.pl/b49962bf Foo would be an opaque struct. createFoo() and destroyFoo() would be implemented in the C library (I know I have to declare them extern(C), this is just an example). As you can see, that code prints 'Destroying Foo' twice, with different pointers. I expected destroyFoo to be called only once, on the instance created by createFoo(), when the Bar instance goes out of scope. Current behaviour causes an invalid pointer to be passed to destroyFoo(). Is this a bug in RefCounted or am I doing something wrong?
proper way to find if attribute present?
enum Bar = Bar; @(Foo) @Bar int x; pragma(msg, __traits(getAttributes, x)); This prints: tuple(Foo, Bar) How do you run code only if Bar is associated with a symbol like x? I was hoping something like this: pragma(msg, hasAnnotation!(x, Bar)); Where getAnnotation from (http://forum.dlang.org/thread/jxbdiwyyxwnmmybiq...@forum.dlang.org) was written as: template hasAnnotation(alias f, Attr) { bool helper() { foreach(attr; __traits(getAttributes, f)) static if(is(attr == Attr) || is(typeof(attr) == Attr)) return true; return false; } enum bool hasAnnotation = helper; } I am missing how this can be used. Thanks Dan
Re: extern template instantiation
On Wednesday, 23 October 2013 at 11:58:17 UTC, Dicebot wrote: Using `enum` with ctRegex is discouraged because it is a dumb copy-paste upon every enum usage. I'd recommend to use global variable initialized during compile-time: ``` auto r = ctRegex!(r^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)b=(\w+)c=(\w+)([\D+=\w+)]*)y=([0-9A-Fa-z]+)z=([0-9A-Fa-z]+)$); ``` Though it is pretty complex ctRegex and compiling it is a good stress test for any compiler :) Using auto was my first try. Unfortunately it has the same issue (slighly faster, but still dumb C/P. So far I didn't find any way to tell the compiler not to compile it if I'm not using it (aside from versions). As the type itself takes ages to compile, any reasonable-sized project with few regexes will not be able to use ctRegex in dev mode. I suppose there's no way to create a function that returns a generic ctRegex object without loosing the advantages ? Note: The regex might seems complex, but actually isn't. It parses an home-made REST API based on query authentification: http://broadcast.oreilly.com/2009/12/principles-for-standardized-rest-authentication.html . To me it sounded like a simple use case.
Re: Bug in RefCounted?
On Thursday, 24 October 2013 at 14:58:21 UTC, Rene Zwanenburg wrote: I'm writing a D wrapper for a C library. I was planning to use RefCounted structs to control the lifetime of objects created by this library. Please check the following example: http://dpaste.dzfl.pl/b49962bf Foo would be an opaque struct. createFoo() and destroyFoo() would be implemented in the C library (I know I have to declare them extern(C), this is just an example). As you can see, that code prints 'Destroying Foo' twice, with different pointers. I expected destroyFoo to be called only once, on the instance created by createFoo(), when the Bar instance goes out of scope. Current behaviour causes an invalid pointer to be passed to destroyFoo(). Is this a bug in RefCounted or am I doing something wrong? I answered a question related to RefCount on SO http://stackoverflow.com/questions/4632355/making-a-reference-counted-object-in-d-using-refcountedt/4635050#4635050 Not written to your specific problem, but may give you the information you need.
Re: extern template instantiation
24-Oct-2013 20:30, Mathias LANG пишет: On Wednesday, 23 October 2013 at 11:58:17 UTC, Dicebot wrote: Using `enum` with ctRegex is discouraged because it is a dumb copy-paste upon every enum usage. I'd recommend to use global variable initialized during compile-time: ``` auto r = ctRegex!(r^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)b=(\w+)c=(\w+)([\D+=\w+)]*)y=([0-9A-Fa-z]+)z=([0-9A-Fa-z]+)$); ``` Though it is pretty complex ctRegex and compiling it is a good stress test for any compiler :) Using auto was my first try. Unfortunately it has the same issue (slighly faster, but still dumb C/P. So far I didn't find any way to tell the compiler not to compile it if I'm not using it (aside from versions). As the type itself takes ages to compile, any reasonable-sized project with few regexes will not be able to use ctRegex in dev mode. I suppose there's no way to create a function that returns a generic ctRegex object without loosing the advantages ? Something to the extent of auto getMeARegex() { static r = ctRegex!(r^(\w+)\s+([a-zA-Z0-9/]+)\?a=(\w+)b=(\w+)c=(\w+)([\D+=\w+)]*)y=([0-9A-Fa-z]+)z=([0-9A-Fa-z]+)$); return r; } _Might_ work. Failing that make it an 0-arg template: auto getMeARegex()(){ ... } Note: The regex might seems complex, but actually isn't. It parses an home-made REST API based on query authentification: http://broadcast.oreilly.com/2009/12/principles-for-standardized-rest-authentication.html . To me it sounded like a simple use case. -- Dmitry Olshansky
Re: Bug in RefCounted?
On 10/24/2013 07:58 AM, Rene Zwanenburg wrote: I'm writing a D wrapper for a C library. I was planning to use RefCounted structs to control the lifetime of objects created by this library. Please check the following example: http://dpaste.dzfl.pl/b49962bf Foo would be an opaque struct. createFoo() and destroyFoo() would be implemented in the C library (I know I have to declare them extern(C), this is just an example). As you can see, that code prints 'Destroying Foo' twice, with different pointers. I expected destroyFoo to be called only once, on the instance created by createFoo(), when the Bar instance goes out of scope. Current behaviour causes an invalid pointer to be passed to destroyFoo(). Is this a bug in RefCounted or am I doing something wrong? Technically, it is a problem with FooWrapper. Regardless of whether RefCounted's behavior, by default, structs in D are freely copyable and movable value types. The compiler can do those things as it sees fit. Since FooWrapper owns a resource, it must also define the post-blit to make a copy of Foo. (As an aside, dmd at git head does not make such a copy.) Ali
Re: running a command in a directory using std.process
+1 this is a command use case. Further,relying on shell such as cd subdir foo is fragile: if it fails, we're not sure whether it's because it couldn't cd to subdir or because of foo. Woudl the following be as efficient? system_in_dir(string dir, string action){ auto path=getcwd scope(exit) chdir(path) chdir(dir) system(action) } On Thu, Oct 24, 2013 at 12:43 AM, Benjamin Thaut c...@benjamin-thaut.dewrote: Am 24.10.2013 09:06, schrieb simendsjo: On Thursday, 24 October 2013 at 06:25:40 UTC, Benjamin Thaut wrote: As far as I can tell std.process can only run commands in the working directory of the currently executing function. I want to execute a certain program inside a subdirectory on windows and can't get it to work: myproject |- subdir So my executable has myproject as working directory. And I want to execute dmd with subdir as working directory. Isn't it possible to execute a command like cd subdir dmd? Thanks. That works. It still would be nice if std.process had this in its API. -- Kind Regards Benjamin Thaut
Re: proper way to find if attribute present?
On Thursday, October 24, 2013 18:22:43 Daniel Davidson wrote: enum Bar = Bar; @(Foo) @Bar int x; pragma(msg, __traits(getAttributes, x)); This prints: tuple(Foo, Bar) How do you run code only if Bar is associated with a symbol like x? I was hoping something like this: pragma(msg, hasAnnotation!(x, Bar)); You should probably use std.traits.functionAttributes: http://dlang.org/phobos/std_traits.html#.FunctionAttribute And in general, you should favor using std.traits over __traits. Ideally, no code outside of the standard library would need to use __traits. That's not currently the case unfortunately, but it is the case that std.traits has much of what you need for anything involving static introspection. - Jonathan M Davis
Re: proper way to find if attribute present?
On Thursday, 24 October 2013 at 17:36:42 UTC, Jonathan M Davis wrote: You should probably use std.traits.functionAttributes: it doesn't list UDAs though.
Re: proper way to find if attribute present?
I use this small helper in vibe.d: === template extractUda(UDA, alias Symbol) { import std.typetuple : TypeTuple; private alias TypeTuple!(__traits(getAttributes, Symbol)) udaTuple; private template extract(list...) { static if (!list.length) enum extract = null; else { static assert (!is(list[0] == UDA), extractUda is designed to look up values, not types); static if (is(typeof(list[0]) == UDA)) enum extract = list[0]; else enum extract = extract!(list[1..$]); } } enum extractUda = extract!udaTuple; } === Used like this: === @Bar(value) void foo(); enum uda = extractUda!(Bar, foo); static if (is(typeof(uda) != typeof(null))) static assert (uda == Bar(value)); === It is far from perfection but can serve as a starting point for more complete implementation (though I have kept certain limitations intentionally)
casting issue
May I cast like: struct Point { float x, y, z; float r, g, b, a; } Point[] points; void foo(float[] float_array) {}; foo(cast(float[]) points); // is it safe? May be more elegant way do express this exists? Thanks
Re: proper way to find if attribute present?
On Thursday, 24 October 2013 at 16:22:44 UTC, Daniel Davidson wrote: template hasAnnotation(alias f, Attr) { This function looks for annotations as identified by type. Instead of using a plain string, you should make them some kind of struct: struct MyAnnotation { string value; } Then you attach it like this: @MyAnnotation(some value) void foo() {} Then you'll be able to get it with the other two functions in there: static if(hasValueAnnotation!(foo, MyAnnotation)) { pragma(msg, getAnnotation!(foo, MyAnnotation)); // prints MyAnnotation(some value) // or you can fetch it into a variable: MyAnnotation a = getAnnotation!(foo, MyAnnotation); assert(a.value == some value); } It is possible to use plain string in place of the MyAnnotation struct: @(some value) void foo() {} static if(hasValueAnnotation!(foo, string)) { pragma(msg, getAnnotation!(foo, string)); // prints MyAnnotation(some value) // or you can fetch it into a variable: string a = getAnnotation!(foo, string); assert(a == some value); } But that isn't as reliable across modules because strings might be reused by anyone. A struct would always have a unique identifier - the struct name, which can be disambiguated by module.
Re: casting issue
On Thursday, 24 October 2013 at 17:59:03 UTC, Alexandr Druzhinin wrote: May I cast like: struct Point { float x, y, z; float r, g, b, a; } Point[] points; void foo(float[] float_array) {}; foo(cast(float[]) points); // is it safe? May be more elegant way do express this exists? Thanks Not entirely. Try adding `align(64)` to struct declaration and observe funny change ;) It is clearly some low-level hack and not something for usage in normal code. One can create array from struct instance via [ point.tupleof ], but this will allocate as all array literals do.
Re: conv text and pure
On Thu, Oct 24, 2013 at 12:12:48AM +0200, Daniel Davidson wrote: [...] Here is the self-contained code (I hope) that you can see it happening in: http://pastebin.com/hb0Dz50r [...] Hmm. Somebody claimed that 2.064 beta has made text() pure, but that's only partially true, because it calls to!(), and so its purity depends on the purity of to!(). Unfortunately, to!() is NOT pure for many basic types, including to!string(double.init). Furthermore, to!string(S) for some struct S appears to be impure by default unless you manually define a toString method that declares itself pure. Unfortunately, to!string(DateTime.init) isn't pure either, which means that if your struct contains DateTime and you need to format it then you're out of luck. So basically, text() is still practically unusable for anything that isn't already trivially convertible to string (like string itself). Or int -- apparently to!string(int.init) does work in pure code. IOW, the purity of text() and to() still has a long ways to go before user code can depend on it. :-( Please file a bug in the bugtracker. In the meantime, I'm going to track down exactly why to!string(double) isn't pure, since there's no valid reason why it can't be. T -- I am Ohm of Borg. Resistance is voltage over current.
Re: conv text and pure
On Thu, Oct 24, 2013 at 11:36:09AM -0700, H. S. Teoh wrote: On Thu, Oct 24, 2013 at 12:12:48AM +0200, Daniel Davidson wrote: [...] Here is the self-contained code (I hope) that you can see it happening in: http://pastebin.com/hb0Dz50r [...] Hmm. Somebody claimed that 2.064 beta has made text() pure, but that's only partially true, because it calls to!(), and so its purity depends on the purity of to!(). Unfortunately, to!() is NOT pure for many basic types, including to!string(double.init). [...] In the meantime, I'm going to track down exactly why to!string(double) isn't pure, since there's no valid reason why it can't be. [...] So I traced the code down in Phobos, and discovered that the reason to!string(double.init) isn't pure is because it ultimately calls the C library snprintf to format floating-point values. And the fact that it uses toString(scope void delegate(const(char)[])), which can't be made pure because its purity depends on the purity of the delegate, and currently the compiler has no way of inferring this, and the language has no way to express this. :-( Which means unless we rewrite large swaths of std.conv, making text(double.init) pure isn't going to happen anytime soon. :-( T -- It is not the employer who pays the wages. Employers only handle the money. It is the customer who pays the wages. -- Henry Ford
Re: casting issue
On Thursday, 24 October 2013 at 17:59:03 UTC, Alexandr Druzhinin wrote: foo(cast(float[]) points); // is it safe? Two options would be to make the points itself be a float[] with the names just properties into the index: struct Point { float[7] data; ref float x() { return data[0]; } ref float y() { return data[1]; } // etc etc etc } then to use it, just pass point.data[] instead of casting. Or you could also do a union: struct Point { union { float[7] data; struct { float x,y,z,r,g,b,a; } } } and again, pass point.data[] instead of casting it, while continuing to use the other members normally. These are both well defined so are less likely to break than the cast.
Re: casting issue
25.10.2013 02:08, Adam D. Ruppe пишет: On Thursday, 24 October 2013 at 17:59:03 UTC, Alexandr Druzhinin wrote: foo(cast(float[]) points); // is it safe? Two options would be to make the points itself be a float[] with the names just properties into the index: struct Point { float[7] data; ref float x() { return data[0]; } ref float y() { return data[1]; } // etc etc etc } then to use it, just pass point.data[] instead of casting. Or you could also do a union: struct Point { union { float[7] data; struct { float x,y,z,r,g,b,a; } } } and again, pass point.data[] instead of casting it, while continuing to use the other members normally. These are both well defined so are less likely to break than the cast. I thought to use union, but using struct with properties is good to know. Thanks for sharing!
Re: running a command in a directory using std.process
Am 24.10.2013 19:03, schrieb Timothee Cour: +1 this is a command use case. Further,relying on shell such as cd subdir foo is fragile: if it fails, we're not sure whether it's because it couldn't cd to subdir or because of foo. Woudl the following be as efficient? system_in_dir(string dir, string action){ auto path=getcwd scope(exit) chdir(path) chdir(dir) system(action) } Well at least the windows API call CreateProcess already has a parameter which you can use to specify the working directory. Its just not exposed in the implementation of std.process. I don't know how things are on linux though. Kind Regards Benjamin Thaut
Re: D / GtkD for SQL Server
On Tuesday, 22 October 2013 at 06:33:04 UTC, John Joyus wrote: That works! Thanks. cool. If you need anything more in this, let me know (feel free to email me directly too destructiona...@gmail.com ) and i'll see what I can do. The 99% of my programs demand a basic GUI. And I personally love to create stand-alone tools that do not have a ton of dependencies, esecially anything that doesn't come with Winodws by default. Yeah, it isn't as commonly used for me (most my programs are actually text or web) but I like lightweight too since it is just so much easier to use on other computers. minigui.d has an (even more incomplete than the Windows parts) implementation on Linux too, but I'm writing that for me and me alone, so it will probably never be great... but can work in a pinch if you ever need that.
Re: Interfacing via Java Native Interface
On Wednesday, 16 October 2013 at 14:39:23 UTC, Andrew wrote: Concluding, I can post the could if you like, or I can first try compiling it for ARM :-) whatever's easiest for you. I have an arm cross compiler targeting the raspberry pi, not sure if that will work for the android device or not, I'll try it later (the device arrived yesterday, but I haven't had much of a chance to play with it yet). But I've never done JNI before either, so I'm starting more or less from scratch here.