Posts tagged bugs

Delphi anonymous methods are …

… too fat.

Surely writing code like this gets frustrating very fast. I want my lambdas!!

  { Lovely, isn't it? }
  LGroupJoined := LOwners.Op.GroupJoin<TPet, TPerson, TOwnedPets>(LPets,
    function(Arg1: TPerson): TPerson begin Result := Arg1; end,
    function(Arg1: TPet): TPerson begin Result := Arg1.FOwner; end,
    function(Arg1: TPerson; Arg2: ISequence<TPet>): TOwnedPets
    begin
      Result.FOwnerName := Format('%s, %s', [Arg1.FLast, Arg1.FFirst]);
      Result.FPetNames := Arg2.Op.Select<String>(
        function(Arg1: TPet): String begin Result := Arg1.FName; end);
    end
  );

  for LOwnedPets in LGroupJoined do
  begin
    WriteLn('Person ', LOwnedPets.FOwnerName, ' owns: ');
    Write('    ');

    for LName in LOwnedPets.FPetNames do
      Write(LName, ', ');

    WriteLn;
  end;

In other news, there is a working version of Collections 2.0 in the SVN. Most of the changes were sitting on my hard drive for a year now. I just made sure that the last touches are applied and that this version compiles in Delphi XE2. I have also applied a few fixes that enabled the package to work in 64 bit environment.

 

Be advised: The unit tests are completely broken. I have no short term plans on making them work. The amount of time needed for that is staggering. I am currently focused on features so I won’t touch them in a while.

 

P.S. I have to say that I am very disappointed with the quality of the IDE when generics are involved. I had countless unpleasant problems in the past but was expecting things to get better in XE2. Not so. My IDE froze countless time when trying to edit some source files. Whenever I would type a “.” or a “(” it would be gone for good. This makes me wonder whether Collections will be usable at all to you guys. With such buggy support I can predict only crashes. And don’t get me started on the EXE size …

TypeInfo workaround

This is going to be a short one. Just wanted to share a simple and elegant work-around for this QC issue:

type
  TypeOf<T> = record
    class function TypeInfo: PTypeInfo; static;
    class function Name: string; static;
    class function Kind: TTypeKind; static;
  end;

{ TypeOf<T> }

class function TypeOf<T>.Kind: TTypeKind;
var
  LTypeInfo: PTypeInfo;
begin
  LTypeInfo := TypeInfo;

  if LTypeInfo <> nil then
    Result := LTypeInfo^.Kind
  else
    Result := tkUnknown;
end;

class function TypeOf<T>.Name: string;
var
  LTypeInfo: PTypeInfo;
begin
  LTypeInfo := TypeInfo;

  if LTypeInfo <> nil then
    Result := GetTypeName(LTypeInfo)
  else
    Result := '';
end;

class function TypeOf<T>.TypeInfo: PTypeInfo;
begin
  Result := System.TypeInfo(T);
end;

Now, you can obtain the type information for any type by simply using TypeOf<T>.TypeInfo:

type
  TRecord<T> = record
    FVal: T;
  end;

  TIntRecord = TRecord<Integer>;

begin
  // TypeInfo(TIntRecord); // Fails with compile-time error;
  WriteLn(GetTypeName(TypeOf<TIntRecord>.TypeInfo));

  ReadLn;
end.

You can also use this construct to obtain the type information for generic types from within themselves:

type
  TSomeRec<T> = record
    function GetMyName: String;
  end;

function TSomeRec<T>.GetMyName(): String;
begin
  // Result := GetTypeName(TypeInfo(TSomeRec<T>)); // Fails
  Result := TypeOf<TSomeRec<T>>.Name;
end;

The only thing to notice is that compile time [DCC Error] E2134 Type ‘XXX’ has no type info errors will not be triggered for types having no type info. Instead, the TypeOf<T>.TypeInfo method will return nil.

DeHL 0.6 available

DeHL

Yes I know I have skipped 0.5. The reality is that 0.5 was due a long time ago, but I did not have enough free time on my hands to complete the unit testing for all new features added. I always try to add as many unit tests as possible to test all possible scenarios.

This release features a general reorganization of some key concepts, new type extension mechanics, boxing, a lot more collections, better support for associative collections through specific Enex extensions … and many more. I am not going to lay out all the changes here since there are quite a few of them. You can view the change log here.

One important note, check out the DeHL.Base.pas unit. There are a number of TOGGLE_XXXXX constants. Toggle them to true or false (if you are using Weaver FT builds) to enable or disable specific functionality. In Tiburon (Delphi 2009) all the features are disabled by default (to be on the safe side).

Get the latest build here. If you stumble upon some bugs please report them here.

Have Fun!

Delphi Generics: Problem 1

Kids, don’t try this at home :)

class procedure DynamicArray<T>.Test();
var
  Arr : array of T;
begin
  { Fails miserably! }
end;

Error:
[DCC Fatal Error] Tests.DynamicArray.pas(772): F2084 Internal Error: URW1135

There you have it :)

The fix is rather simple but annoying:

Declare a new type in the class/record like this:

type
  DynamicArray<T> = class
  private
  type
    TArrayOfT = array of T;

  public
    { Lots of stuff here }
end;

And then simply make the variable Arr to be TArrayOfT:

class procedure DynamicArray<T>.Test();
var
  Arr : TArrayOfT;
begin
  { Works! }
end;

Till the next bug.