在变体记录中包含方法的语法是什么?
我有以下记录定义
E3Vector3T = packed record
public
x: E3FloatT;
y: E3FloatT;
z: E3FloatT;
function length: E3FloatT;
function normalize: E3Vector3T;
function crossProduct( const aVector: E3Vector3T ): E3Vector3T;
class operator add( const aVector1, aVector2: E3Vector3T ): E3Vector3T;
class operator subtract( const aVector1, aVector2: E3Vector3T ): E3Vector3T;
class operator negative( const aVector: E3Vector3T ): E3Vector3T;
class operator multiply( const aVector: E3Vector3T; const aScalar: E3FloatT ): E3Vector3T;
class operator divide( const aVector: E3Vector3T; const aScalar: E3FloatT ): E3Vector3T;
end;
我想做的是引入一个变体记录部分,以便能够单独和作为数组访问三个元素,即
E3Vector3T = packed record
public
case boolean of
true: (
x: E3FloatT;
y: E3FloatT;
z: E3FloatT;
);
false: (
elements: packed array[0..2] of E3FloatT;
);
function length: E3FloatT;
..
end;
这不会编译(函数需要结果类型在函数长度)。我做错了什么明显的事情,或者这不受支持?在这种情况下,对于以数组形式访问各个字段的优雅而高性能的方式有什么建议吗?
ps E3FloatT 是 Single 的简单类型别名。
I have the following record definition
E3Vector3T = packed record
public
x: E3FloatT;
y: E3FloatT;
z: E3FloatT;
function length: E3FloatT;
function normalize: E3Vector3T;
function crossProduct( const aVector: E3Vector3T ): E3Vector3T;
class operator add( const aVector1, aVector2: E3Vector3T ): E3Vector3T;
class operator subtract( const aVector1, aVector2: E3Vector3T ): E3Vector3T;
class operator negative( const aVector: E3Vector3T ): E3Vector3T;
class operator multiply( const aVector: E3Vector3T; const aScalar: E3FloatT ): E3Vector3T;
class operator divide( const aVector: E3Vector3T; const aScalar: E3FloatT ): E3Vector3T;
end;
What I wanted to do is introduce a variant record part to be able to access the three elements both individually and as an array, i.e.
E3Vector3T = packed record
public
case boolean of
true: (
x: E3FloatT;
y: E3FloatT;
z: E3FloatT;
);
false: (
elements: packed array[0..2] of E3FloatT;
);
function length: E3FloatT;
..
end;
This will not compile (function needs a result type at function length). Anything obvious I'm doing wrong, or is this not supported? In that case, any suggestions for an elegant yet performant way of accessing the individual fields as an array?
p.s. E3FloatT is a simple type alias for Single.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这是编译器的疏忽,但当方法在变体部分之前声明时,它确实会编译。这似乎是一个合理的解决方案。
Maybe it is an oversight in the compiler, but it does compile when the methods are declared before the variant part. This seems a reasonable solution.
将函数声明移至顶部,如下所示:
这在 Delphi 2010 中进行编译。
Move the function declaration to the top like this:
This compiles in Delphi 2010.