agent - 从内联代理访问局部变量

标签 agent eiffel

我正在尝试实现 does_key_exist,我认为下面的代码可以做到这一点。但我收到编译错误 key not find 行:Result := not (x.key = key)

does_key_exist(key: attached STRING):BOOLEAN
    do
        Result := not data.item(hash(key)).for_all (agent (x:like pair_at):BOOLEAN
            do
                Result := not equal(x.key, key)
            end)
    end

定义:

pair_at(key:attached STRING):TUPLE[key:STRING;value: like value_at]
    require
        does_key_exist(key)
    deferred
    ensure
    end

list_at(key:STRING) : LINKED_LIST[like pair_at]
        require
            does_key_exist(key)
        end

data : ARRAY[like list_at]

最佳答案

Eiffel 中的内联代理可以访问当前对象的属性,但不能访问局部变量或参数,因为它们只是“正常”代理的语法糖,是根据类的正常特征构建的。后者无法访问其他功能的局部变量或参数。因此可以通过显式传递参数来更正代码:

does_key_exist (key: STRING): BOOLEAN
    do
        Result := not data.item (hash (key)).for_all
            (agent (x: like pair_at; y: STRING): BOOLEAN
                do
                    Result := not equal(x.key, y)
                end
            (?, key))
    end

关于agent - 从内联代理访问局部变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11120133/

相关文章:

linux - 无法在我的 Mac 上安装 WALinuxAgent

sql-server - 如何在没有作业运行时自动停止 SQL Server 代理?

algorithm - 编码空格删除时如何删除最后一个空格

class - 无法解决 Eiffel 中的错误 “target of assigner call has no associated assigner command”

excel - AnyLogic:在模拟启动时用代理填充托盘架

lotus-notes - 如何在 Lotus Notes 中的本地数据库上运行计划代理

machine-learning - 如何保存经过训练的强化学习代理以避免每次都对其进行训练?

c - eiffel c编译失败: error LNK2001: unresolved external symbol

variables - 如何在 Eiffel 的声明中初始化局部变量?