unit Myc.Data.Types.Arrays; interface uses System.Generics.Collections, System.SysUtils, Myc.Data.Types; type TImplDataArrayValue = class; // fwd // Implements the array data type. TImplDataArrayType = class(TInterfacedObject, IDataType, IDataArrayType) private FElementType: IDataType; function GetElementType: IDataType; function GetName: String; function GetKind: TDataKind; public constructor Create(const AElementType: IDataType); function CreateValue(const AItems: array of IDataValue): IDataArrayValue; end; // Implements the array data value. TImplDataArrayValue = class(TInterfacedObject, IDataValue, IDataArrayValue) private FArrayType: IDataArrayType; FItems: TArray; function GetDataType: IDataType; function GetElementCount: Integer; function GetItem(Idx: Integer): IDataValue; public constructor Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue); end; implementation uses System.SyncObjs; { TImplDataArrayValue } constructor TImplDataArrayValue.Create(const AArrayType: IDataArrayType; const AItems: array of IDataValue); var i: Integer; begin inherited Create; FArrayType := AArrayType; SetLength(FItems, Length(AItems)); for i := 0 to High(AItems) do FItems[i] := AItems[i]; end; function TImplDataArrayValue.GetDataType: IDataType; begin Result := FArrayType; end; function TImplDataArrayValue.GetElementCount: Integer; begin Result := Length(FItems); end; function TImplDataArrayValue.GetItem(Idx: Integer): IDataValue; begin Result := FItems[Idx]; end; { TImplDataArrayType } constructor TImplDataArrayType.Create(const AElementType: IDataType); begin inherited Create; FElementType := AElementType; end; function TImplDataArrayType.CreateValue(const AItems: array of IDataValue): IDataArrayValue; var item: IDataValue; i: Integer; begin // Validate that all items match the element type of this array type. i := 0; for item in AItems do begin if (item.DataType <> FElementType) then raise EArgumentException.CreateFmt( 'Invalid data type for item at index %d. Expected ''%s'', but got ''%s''.', [i, FElementType.Name, item.DataType.Name]); inc(i); end; Result := TImplDataArrayValue.Create(Self, AItems); end; function TImplDataArrayType.GetElementType: IDataType; begin Result := FElementType; end; function TImplDataArrayType.GetName: String; begin Result := Format('Array<%s>', [FElementType.Name]); end; function TImplDataArrayType.GetKind: TDataKind; begin Result := dkArray; end; end.