英文:
Is there any equivalent to the "final" keyword from C++ in DML?
问题
在DML中是否有与C++中的final等效的内容?
在DML中没有与C++中的final关键字等效的内容。
英文:
I currently have a register template that defines a method called read_action that I am looking to deprecate. I'd ideally like to decorate this method in the base template in such a way that no other template or register/field that imports this template can define read_action() and cause a compiliation error if they do. In C++ I could have achieved this by decorating it with the final keyword in C++.
template my_read is (read) {
shared method read_action(uint64 in_value) -> (uint64) throws default {
return in_value;
}
}
Is there any equivalent to final in DML?
答案1
得分: 1
DML 方法默认是最终的,因此你的 C++ 技巧的直接等价物就是删除 default
关键字:
method read_action(uint64 in_value) -> (uint64) throws {
log error: "调用已弃用的 %s.read_action", qname;
}
由于这个方法不是默认的,任何尝试覆盖它的操作都会在编译时引发错误;此外,对该方法的任何调用都会在运行时引发错误。
更进一步的改进是毒害符号 read_action
:
param read_action = undefined; // 不要调用或覆盖 read_action,它已被弃用
这会在编译时对覆盖和使用该符号都产生错误。此外,覆盖的 DMLC 错误消息将指出这一行,使注释成为补充的错误消息。
实际上,在 DML 1.2 标准库中,这种技巧确实用于一些奇特的符号(例如 dev.banks
、dev.log_group
),这些符号在 DML 1.4 中已被弃用。
英文:
DML methods are final by default, so the direct equivalent of your C++ trick would be to just remove the default
keyword:
method read_action(uint64 in_value) -> (uint64) throws {
log error: "call to deprecated %s.read_action", qname;
}
Since this method is not default, any attempt to override will give a compile error; furthermore, any call to the method will give an error in run-time.
A further improvement is to just poison the symbol read_action
:
param read_action = undefined; // don't call or override read_action, it is deprecated
This gives compile-time errors both for overriding and using the symbol. Furthermore, the DMLC error message of an override will point out this line, making the comment a complementary error message.
This trick is in fact employed for some esoteric symbols in the DML 1.2 standard library (e.g. dev.banks
, dev.log_group
), that were deprecated for DML 1.4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论