Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, 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.

like image 907
Paul-Jan Avatar asked Mar 09 '10 21:03

Paul-Jan


People also ask

What is variant programming language?

A Variant is a special data type that can contain any kind of data except fixed-length String data. (Variant types now support user-defined types.) A Variant can also contain the special values Empty, Error, Nothing, and Null.

What is the Variant data type in VBA?

The Variant data type is automatically specified if you don't specify a data type when you declare a constant, variable, or argument. Variables declared as the Variant data type can contain string, date, time, Boolean, or numeric values, and can convert the values that they contain automatically.

Which of the following is a disadvantage of Variant data type?

The disadvantages are that certain pieces of code using variant may process a little more slowly than others, or may not catch certain classes of programming errors. The default data type is variant. Variant is a self-describing data type that can hold simple numeric or string values.

What is the size of Variant data type?

The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length),and can store any character text.


2 Answers

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;
like image 139
Lars Truijens Avatar answered Oct 24 '22 09:10

Lars Truijens


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.

like image 25
Ville Krumlinde Avatar answered Oct 24 '22 10:10

Ville Krumlinde