d别名触发错误过时消息

struct Module
{
    CachedString data;
}

struct CachedString
{
    private size_t len;

    this (string data) { this.len = data.length; }
    public string str () const { return null; }
    public void str (string value) {
        this.len = value.length; assert(0); 
    }

    alias str this;
}

void main ()
{
    Module m;
    m.data = "你好";
}

消息说,不能用别名来部分初化m.data,用m.data.str,消息是错的.
不能分配缓存串.唯一成员是长度及函数中的别名本.


如果编译器可以编译它,并不表明应该这样.
别名设置器不打算自定义赋值,别名取很有用,且不能替代.而别名置则与opAssign部分重复.


补充测试:

EntryType arr;
auto getPtr() { return &arr; } 

struct EntryType {
    //int somethingelse; //为了不同错误信息,而添加
    bool _state;
    alias _state this;
}

struct S1 {
    @property auto ref entry() { return *getPtr(); }
    alias entry this;
}

void main(){
    S1 s1;
    s1 = true; //不能用`别名本`来部分初化
               //S1`类型的`s1`变量.用`s1.entry()._state`
}

是因为op_overload是递归的,因为检查条件找错东西了.

if (Expression result = checkAliasThisForLhs(ad1, sc, e))
{
    // Problem #1
    //   + e = `m.data = "Hello World"`
    //   + result = `m.data.str("Hello World")
    // 我们应查找`result{.e1}.op`,而非原始式
    if (e.op != EXP.assign || e.e1.op == EXP.type)
        return result;

    // Problem #2
    //   + ad1 = `S1`
    //   + result.var.parent = `EntryType`
    // 我们应查找`重写结果`的`lhs`聚集声明
    if (ad1.fields.dim == 1 || (ad1.fields.dim == 2 && ad1.vthis))
    {
        if (var && var.type == ad1.fields[0].type)
            return result;

        if (tf.isref && ad1.fields[0].type == tf.next)//这句话,感觉无意义
            return result;
    }
}

什么,应该是静态的正确行为?

struct Test
{
    private static int _impl;
    static ref int state() { return _impl; }
    void check(int expected) { assert(_impl == expected); }
    alias foo this;
}

struct Test2
{
    Test test;
    alias test this;
}

Test t;
t = 42;        // foo() = 42
t.check(42);

Test2 t2;
t2 = 42;       // foo() = 42;
t2.check(42);

规范不详细,直觉是不要过时它,我们只关心DotVarExp.

posted @   zjh6  阅读(8)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示