在我看来,功能纯度的强大之处在于深度代码路径可以被验证为无副作用.人们在代码树规模上的经验是什么,可以在纯粹的说明符中,以及代码重用的级别是什么?
我发现的一些事情:
std.algorithm大部分都没有被标记为pure,但可能很大程度上是纯粹的,要么是纯粹的算法需要实例化函数或mixin的纯度,要么纯度说明符本身是静态多态的.
有用的转换器to!string( someInt )目前不是纯粹的.
用户定义的结构似乎有问题(如下所示):
1.嵌套结构上的纯析构函数
2.纯粹的postblit函数,即使在非嵌套结构上也是如此
以下代码目前在DMD 2.052 win 32-bit上给出了多个错误
struct InnerStruct
{
pure this(this) {}
pure ~this() {}
}
struct OuterStruct
{
InnerStruct innerStruct;
pure this(this) {}
pure ~this() {}
}
pure void somePureFunc()
{
OuterStruct s1 = OuterStruct(); // pure nested destructor does not compile
OuterStruct s2 = s1;
InnerStruct is1 = InnerStruct(); // pure non-nested destructor seems to compile
InnerStruct is2 = is1; // pure non-nested postblit does not …Run Code Online (Sandbox Code Playgroud) 给定D中的函数声明,是否可以在编译时自省任何函数参数名称的字符串表示,以用于自动函数反射.例如
void foo(int a, double b, string c) { }
register_function!(foo)()
Run Code Online (Sandbox Code Playgroud)
register_function能否在编译时以与__traits(allMembers,someClass)类似的方式提取"a","b","c"?
以下给我一个Windows 32位,dmd.2.052,没有标志的访问冲突.当析构函数由垃圾收集器运行时,消息框似乎在此过程中被破坏.
import std.stdio;
import core.thread;
import core.memory;
import std.concurrency;
class C
{
string m_str;
Tid m_receiverTid;
this(string s, Tid rt) { this.m_str = s; this.m_receiverTid = rt; }
~this() { writeln("Destructor : ",this.m_str);
m_receiverTid.send(this.m_str);
}
}
void receiver() {
try {
while(true) {
receive((string s){writeln("Received: ",s);});
}
} catch (Throwable th) {
writeln("Caught throwable: ",th.toString());
}
}
void main() {
Tid receiverTid = spawn(&receiver);
receiverTid.send("Basic test");
Thread.sleep( 5_000_000 );
C c1 = new C("c1 Manually deleted",receiverTid);
delete c1;
Thread.sleep( …Run Code Online (Sandbox Code Playgroud) 没有更多引用的对象不能立即使用GC.collect()进行垃圾回收,但是例如new,writeln或Thread.sleep的中间调用将使得未引用的对象可以通过GC.collect()访问.
import std.stdio;
import core.thread;
import core.memory;
class C
{
string m_str;
this(string s) {this.m_str = s;}
~this() { writeln("Destructor: ",this.m_str); }
}
void main()
{
{
C c1 = new C("c1");
}
{
C c2 = new C("c2");
}
//writeln("Adding this writeln means c2 gets destructed at first GC.collect.");
//Thread.sleep( 1 ); // Similarly this call means c2 gets destructed at first GC.collect.
//int x=0; for (int i=0; i<1000_000_000;++i) x+=2*i; // Takes time, but does not make c2 get …Run Code Online (Sandbox Code Playgroud)