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];
//好处:不使用堆.
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现