如何为编译时索引实现 opIndex?
How to implement opIndex for compile time indices?
ref auto opIndex(size_t i){
return t[i];
}
这里t
是一个元组,i
需要在编译时读取。我如何用 D 表达这个?
目前 opIndex
没有任何干净的方法可以做到这一点,原因有二。首先很简单 - 它没有实现。这将相对容易自行修复,但还有第二个原因 - 它为语言语法增加了严重的上下文敏感性。
考虑这个结构定义:
struct S
{
// imagine this works, syntax is not important
static int opIndex (size_t i) { return 42; }
}
代码 S[10]
是什么意思?它是十个 S
元素的静态数组类型吗?还是静态opIndex
调用哪个returns42
?在不了解很多上下文的情况下是不可能分辨的,在某些情况下根本不可能分辨(比如 typeof(S[10])
)。
有点相关(未批准!)的想法:http://wiki.dlang.org/DIP63
ref auto opIndex(size_t i){
return t[i];
}
这里t
是一个元组,i
需要在编译时读取。我如何用 D 表达这个?
目前 opIndex
没有任何干净的方法可以做到这一点,原因有二。首先很简单 - 它没有实现。这将相对容易自行修复,但还有第二个原因 - 它为语言语法增加了严重的上下文敏感性。
考虑这个结构定义:
struct S
{
// imagine this works, syntax is not important
static int opIndex (size_t i) { return 42; }
}
代码 S[10]
是什么意思?它是十个 S
元素的静态数组类型吗?还是静态opIndex
调用哪个returns42
?在不了解很多上下文的情况下是不可能分辨的,在某些情况下根本不可能分辨(比如 typeof(S[10])
)。
有点相关(未批准!)的想法:http://wiki.dlang.org/DIP63