Why foreach is not nothrow even for a simple associative array?

2011-10-13 Thread Cheng Wei
import std.c.stdio int[int] g_map; void main() { test(); } nothrow void test() { foreach(k, v) { printf(%d, %d\n, k, v); } } Cannot compile with the error: Error: _aaApply2 is not nothrow It is not so convenient that we have to use try-catch for all iteration even when we

Re: Why foreach is not nothrow even for a simple associative array?

2011-10-13 Thread Cheng Wei
Thanks. The problem is that if we tag _aaApply2 as nothrow, then can we still put throwable codes in the body of foreach?

does private import means static import to other modules importing it?

2011-10-13 Thread Cheng Wei
suppose we have in module A, we have import B; Then in module C; import A; B.fun1(); B.fun2(); can be compiled successfully even without static import B. Is this correct? Thanks.

Re: Why foreach is not nothrow even for a simple associative array?

2011-10-13 Thread Cheng Wei
Yeah, the problem could be solved if _aaApply2 was a template instead of a function. Alternatively, maybe the compiler can skip the purity/throwbility check for __aaApply2 and directly check the statements inside. That means to treat the compiler generated function __aaApply2 differently than the

Re: compare struct with string member is wield;

2011-10-12 Thread Cheng Wei
Sorry. The previous is not the one causes the problem. Try this: struct S { string str = Hello; // Adding an initial value here. }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1); // Fail auto s3 = g_s; assert(s3 == g_s);; // Even this

Re: compare struct with string member is wield;

2011-10-12 Thread Cheng Wei
Thanks. Removing the ';' after struct and class is really helpful. The ; keeps trapping me in C++ coding. :)

operator ~ does not check type?

2011-10-12 Thread Cheng Wei
The following expression compiles but does not make sense. string str = hello ~ 10; assert(str == hello\n); Is this a useful feature or just a bug?

compare struct with string member is wield;

2011-10-11 Thread Cheng Wei
struct S { string str; }; S g_s; unittest { S s1; S s2; assert(s1 == s2); // Success assert(g_s == s1);// Failed } Is this expected? If so, may I know the reason? Thanks.

Re: Why an abstract pointer cannot be used as value in an associate array?

2011-09-29 Thread Cheng Wei
The problem is that the void* cannot convert back to AB* when we want to use it in c library. Just don't understand why the cast(AB*)p (p is void *) needs to know the size of AB. Is there any unsafe cast which can blindly cast the pointers?

Re: Why an abstract pointer cannot be used as value in an associate array?

2011-09-29 Thread Cheng Wei
Thanks a lot. This solves the problem. However, it breaks the abstractness. Now in D side, we can call auto v = ab(). This does not make sense, because then v cannot be used in the C library. I don't understand why when we manipulate AB*, D compiler needs to know the size of struct ab. Moreover,

why global immutable string variable cannot be used after case?

2011-09-23 Thread Cheng Wei
import std.stdio; immutable HELLO = hello; void main() { auto string = hello; switch(string) { case HELLO: writeln(hello); break; default: writeln(unknown); break; } } testCase.d(7): Error: case must be a

Why this simple binaryHeap program does not compile?

2011-09-22 Thread Cheng Wei
import std.container; class T { int i; } void main() { auto array = Array!(T); auto heap = BinaryHeap!(Array!(T), a.i b.i)(array); } After compiling: dmd2/linux/bin32/../../src/phobos/std/container.d(1612): Error: template std.conv.emplace(T) if (!is(T == class)) does not match

Is there a profiler for D2?

2011-09-20 Thread Cheng Wei
Is there any usable profiler for D2?

How to read output of a script

2011-09-20 Thread Cheng Wei
In D2, how can we get the output of running a script. We can use 'system' to run the script, but how we cannot assign the standard output of the running script. Is there any way to do it, or whether it will be included into future version of std.process? Thanks a lot.

Is this a bug in execvp of std.process

2011-09-20 Thread Cheng Wei
#import std.process void main() { execvp(ip, route); } result: Object ute is unknown, try ip help. That is the first two bytes are lost Adding two spaces works: #import std.process void main() { execvp(ip, route); } Version 2.055, linux, 32bit Thanks.

Is it a bug in execvb (std.process)?

2011-09-20 Thread Cheng Wei
import std.process; void main() { execvp(ip, [route]); } result: Object ute is unknown, try ip help. So the first two bytes are lost. After adding two spaces in the first argument, it works. import std.process; void main() { execvp(ip, [ route]); } dmd 2.055 linux 32bit. Is this a bug?

Re: How to read output of a script

2011-09-20 Thread Cheng Wei
Thanks a lot. Weird. It is not in the library reference in http://www.d-programming- language.org/, but it is in the library reference in digitalmars.com. I throught the previous one was the official web site now. It seems it still is not synced well.

How to filter an array so the result is an array again?

2011-09-15 Thread Cheng Wei
The standard library std.algorithm is based on Range. So if a = [1, 2, 3, 4]; auto r = filter!(a 2)(a); Here, r is a range. How about I want an new array? Is there any easy way to convert the result to array? If we have to do like: int[] b; for (v; r) { b ~= v; } Then maybe it is easier to

Re: How to filter an array so the result is an array again?

2011-09-15 Thread Cheng Wei
Sorry, the 'for' should be 'foreach'.