d解决GC问题的一个提议
原文
 昨晚我想出个主意.
 如果存储器(变量,函数参数,构或类字段),在编译时存储类型,为什么不在编译时存储分配器呢?
 也即,既不在类型也不在值中,而是按属性存储分配器信息.
首先,它引入了新的有趣的惯用法(如"new void","new __stack")等.
 其次,如果可能,它不会生成大量模板代码,也不会占用内存中的值.
 三,是可读性强.
 最大好处是,为了重写标准库和运行时,只需要把新属性添加到函数参数中,且有一半可能,会管用!
在其他语言,我还没有看到它!在beeflang中,分配器开发得很好,但它不存储分配器,而是交给用户处理.
 怎么样?
//语法
newAttribute:
  new aliasValue
  new extern aliasValue
  /*仅在函数中*/
    new
    new: identifier
 //`aliasaliasValue`可以是别名可接受的值,或者是保留的分配器(`__GC,__stack)`,或者是`void`关键字
/***变量***/
/*new*/{
  string text new myAlloc;
  /*等价于
    scope string text new extern myAlloc; 
    //'extern'不需要生成scope(exit)和scope(exit)来析构(text);
  */
  text.length = 90; // ok
  string text2 new myAlloc;
  text2 = text; // error!
  text2 = new string(text); //ok
  text2 = move(text); //ok
}
/*new extern*/{
  string text new extern myAlloc;
  string textGC = text; //error: __GC != myAlloc
  /*
    string textGC new extern __GC;
  */
  string text2 new extern myAlloc;
  text2 = text; // OK!
  text2.length = text2.length + 5;
  assert(text2.ptr != text1.ptr);
  destroy(text2);
  destroy(text1);
}
/***构***/
{
  struct Person{
    string name;
    int age;
  }
  Person likesGC;
  Preson hatesGC new myAlloc;
  //伪码
  static assert(__traits(isSame, allocof(hatesGC), allocof(hatesGC.name)));
  static assert(__traits(isSame, allocof(likesGC), allocof(likesGC.name)));
}
{
  struct Sample{
    IAllocator alloc;
    string text new alloc; // 为何不?
  }
  Sample instance;
}
{
  struct Sample{
    void method() {
      string text new allocof(this);
      pragma(msg, allocof(this));
 //ERR: __alloc - 运行时函数参数
    }
  }
  Sample sample new myAlloc;
  sample.method();
}
{
  struct Sample{
    void method() new thisAlloc{
      string text new thisAlloc;
      pragma(msg, thisAlloc); //OK
    }
  }
  Sample sample new myAlloc;
  sample.method();
}
/***函数***/
/*
//不生成附加代码,只是`检查`类型
*/
void someAlloc(ref char[] name new){
  name.length = 4;
  name[] = "jack";
  pragma(msg, allocof(name));//错误,编译时未知.
}
void noAlloc(char[] new void){
  assert(name.length == 4);
  name[] = "jack";
}
 //`"new void"`表示`allacator`类型未知,并且无法调用要求此结构`allacator`的方法
void noAllocStruct(Person a new void){
  a.age = 0; //ok
  a.name = "Walter"; ///错误,分配器.
}
void multiArgs(string a new, string b new);
 //创建类似标签的内容,调用者检查存储分配器
string new a1 multiArgsSameAllocator(string a new: a1, string b new: a1){
  string text new a1;
  pragma(msg, a1);//ERR a1 - AllacatorI
  pragma(msg, typeof(a1));//=> AllacatorI
  return a ~ b;
}
//编译时已知分配器
//生成代码
void sample(alias Alloc)(string text new Alloc)
{
  pragma(msg, allocof(text)); //OK
}
//ABI
void fu_one(string a new, int n)
->
void fu_one(string a, int n, IAllocator __a_alloc)
void fu_multi(string a new, string b new, int n)
->
void fu_multi(string a, string b, int n, ushort __a_id, ushort __b_id, IAllocator[] __allocs...)
/***奖金***/
{
  string dtext;
  char[] ctext new __stack = dtext.toStringz!__stack; //c库中分配
  ctext ~= "boo"; // error
  ubyte[] result = someCShit(ctext)[0..length];
  //好处:不使用堆.
}
                
                
            
        
浙公网安备 33010602011771号