Talk:Generics proposals

From Lazarus wiki
Jump to navigationJump to search

Generics may not be powerful enough

Hi, I'm interested in porting QuickCheck, a dynamic unit test framework, to Free Pascal. I've been able to port it to other languages with varying degrees of difficulty (JavaScript, Smalltalk, C, Factor, Chicken Scheme, ...). See [1]

The ease of porting QuickCheck to a particular programming language seems to be the level of functional programming the language provides. Languages with "lambdas", "closures", "continuations", or "code blocks" generally have an easy time with this. Free Pascal does not appear to support lambdas, and I wonder if it would be easier with Free Pascal generics. But from the look of generics, they may not be powerful enough!

Specifically, QuickCheck works by evaluating properties of your code. A property is a simple expression such as "all dogs in the pet database have four legs". QuickCheck exposes a function in its API called for_all() that generates hundreds of random test cases for a given property.

  quickcheck.for_all(lambda pet_database, dog -> return dog.#legs == 4)

In other words, for_all is designed to be able to generate test cases for the particular type of property to be tested. If the property is "all integers are even", then for_all is able to know that it must generate random integers and pass them individually to a property such as even?(int). for_all must be able to handle complex properties of unknown type and unknown arity; it's isomorphic to the problem of implementing Lisp's (apply f '(list of arguments)) in Pascal. And that is really really hard to program in the current version of Free Pascal. If it sounds impossible, it's not: I managed to hack it in C ( But I don't know where to begin in Free Pascal.

If anyone is interested in this unit test framework, my planned Free Pascal port is hosted at GitHub ( It's currently able to generate simple test values, but I don't know how to either A) implement for_all given Free Pascal's emphasis on statically typed imperative programming or B) convince the Free Pascal community of the benefits of untyped lambdas and other functional programming techniques.


Please post questions/remarks on the forum/mailing list. Suggest to remove the comment above in 6 months' time (in order to give Mcandre the chance to post his questions...

I wouldn't allow this

   intlist: GList<Integer>;
   intlist := GList<Integer>.Create;


   tintlist: GList<Integer>;
   intlist: tintlist; 
   intlist := tintlist.Create;

This makes parsing easier and is in the spirit of pascal.

--FPK 09:52, 3 Mar 2005 (CET)

Solution in Borland's QC


 TList<_t> = class
   Data: array of _t;
   procedure Add(Item: _t);
   function Extract(Item: _t): _t;


 TComponentList = TList<TComponent>;
 TIntegerList = TList<Integer>;


 x: TComponentList;
 y: TIntegerList;


 x := TComponentList.Create;
 y := TIntegerList.Create;