Why am I receiving an error about Delphi incompatible types (array and dynamic array)?

前端 未结 1 1274
天涯浪人
天涯浪人 2020-12-11 21:26

(EDIT: This is following on from Are objects reference counted in Windows-targeted Delphi applications, and if so, what is its purpose? and Dynamic arrays a

相关标签:
1条回答
  • 2020-12-11 21:48

    You cannot assign an open array to a dynamic array. See Open Array Parameters.

    Note: The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. The previous example creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. To declare parameters that must be dynamic arrays, you need to specify a type identifier:

    type TDynamicCharArray = array of Char;
    function Find(const A: TDynamicCharArray): Integer;
    

    A good summary of the use case of open arrays and the difference with a dynamic array can be found here: Open array parameters.


    If you have a Delphi version that supports generics, it is possible to declare the constructor header:

    constructor TGenericHoldingResultSet.Create(parent : TGenericHoldingSummary; 
      const resArr : TArray<TGenericHoldingResult>);
    

    and your resultArray as TArray<TGenericHoldingResult>.

    This will avoid having to declare a specific type for the array.

    As noted by David, open arrays have a benefit since they have a broader use case, and should be used when possible.

    0 讨论(0)
提交回复
热议问题