Download objects
Transcript
124 - 05: Arrays and Records var List: array [1..10] of Integer; X, I: Integer; begin // initialize the array for I := Low (List) to High (List) do List [I] := I * 2; // call X := Sum (List); Show (X.ToString); // pass portion of the array X := Sum (Slice (List, 5)); Show (X.ToString); Type-Variant Open Array Parameters Besides these typed open array parameters, the Object Pascal language allows you to define type-variant or untyped open arrays. This special kind of array has an undefined number of elements, but also an undefined data type for those elements along with the possibility of passing elements of different types. This is one of the limited areas of the language that is not fully type safe. Technically, the you can define a parameter of type array of const to pass an array with an undefined number of elements of different types to a function. For example, here is the definition of the Format function (we'll see how to use this function in Chapter 6, while covering strings, but I've already used it is some demos): function Format (const Format: string; const Args: array of const): string; The second parameter is an open array, which receives an undefined number of values. In fact, you can call this function in the following ways: N := S := Show Show Show 20; 'Total:' ; (Format ( 'Total: %d' , [N])); (Format ( 'Int: %d, Float: %f' , [N, 12.4])); (Format ( '%s %d', [S, N * 2])); Notice that you can pass a parameter as either a constant value, the value of a vari able, or an expression. Declaring a function of this kind is simple, but how do you code it? How do you know the types of the parameters? The values of a type-variant open array parameter are compatible with the TVarRec type elements. Marco Cantù, Object Pascal Handbook