简体   繁体   English

在变体记录中包含方法的语法是什么?

[英]What's the syntax for including methods in a variant record?

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, ie 我想要做的是引入一个变体记录部分,以便能够单独和作为一个数组访问这三个元素,即

  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? 在这种情况下,任何建议以优雅而高效的方式访问单个字段作为数组?

ps E3FloatT is a simple type alias for Single. ps E3FloatT是Single的简单类型别名。

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. 这似乎是合理的解决方案。

E3Vector3T = packed record
  public
      function length: E3FloatT;
      ..

      case boolean of
          true: (
              x: E3FloatT;
              y: E3FloatT;
              z: E3FloatT;
          );
          false: (
              elements: packed array[0..2] of E3FloatT;
          );
  end;

Move the function declaration to the top like this: 将函数声明移动到顶部,如下所示:

  E3Vector3T = packed record
  public
      function length: E3FloatT;
      case boolean of
          true: (
              x: E3FloatT;
              y: E3FloatT;
              z: E3FloatT;
          );
          false: (
              elements: packed array[0..2] of E3FloatT;
          );

  end;

This compiles in Delphi 2010. 这在Delphi 2010中编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM