The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,466 other followers

Archive for April 21st, 2011

Delphi Constraints in Generics – RAD Studio XE documentation wiki

Posted by jpluimers on 2011/04/21

Quite a few things are similar when doing Generics in both Delphi and C#.

But constraints are a bit different.

To quote the Delphi Wiki page on Constraints in Generics – RAD Studio XE:

Constraints can be associated with a type parameter of a generic. Constraints declare items that must be supported by any particular type passed to that parameter in a construction of the generic type

Constraint items include:

  • Zero, one, or multiple interface types
  • Zero or one class type
  • The reserved word “constructor“, “class“, or “record

You can specify both “constructor” and “class” for a constraint. However, “record” cannot be combined with other reserved words.
Multiple constraints act as an additive union (“AND” logic).

A few other differences are these:

  • In .NET, there is a unified typing system: everything descends from System.Object, including both value types and reference types. In Delphi this is not the case. In .NET, when you don’t specify a constraint, but use the type in a reference way, it will do automatic boxing. Delphi has no concept of automatic boxing, so you cannot use a non-constrained type as a reference
  • In both .NET and Delphi, you cannot specify a constraint on an enumeration type. In .NET you can work around this by constraining on the underlying interfaces. In Delphi you cannot do that because enumeration types are ordinal types that do not implement interfaces.
  • In Delphi you cannot constrain on ordinal types.

Note that – like the Delphi documentation – the C# constraints on type parameters documentation is not complete on those either.

Related: a great post [WayBack] Using Generics in Delphi – Phil Gilmore covering the comparison between C# and Delphi in more depth.

–jeroen

via: Constraints in Generics – RAD Studio XE.

Posted in C#, Delphi, Development, Software Development | 10 Comments »

 
%d bloggers like this: