A note from QA Portal : Generics constrained to (managed) records

Description

  • Compiler gives error ‘Type parameter 'T' must be a non-nullable value type’ when using generic records.

KM 14/Mar/25

program Project2;
uses System.SysUtils;
type
  TTestRecordConstraint = class
    public
      class function Get<T: record>(aKeyValues: Array of const): T;
  end;

  TTestRec = record
      int: integer;
      someThing: string;
  end;

class function TTestRecordConstraint.Get<T>(aKeyValues: array of const): T;
begin
//
end;

begin
  TTestRecordConstraint.Get<TTestRec>(['test']);
end.

EK 17/Mar/25 9:08 PM

  • It is regression from 12.2, so it cannot be “Works as intended”. And with string field it is still a record.

RL 26/Mar/25 6:25 AM

  • Agreed. The documentation for the “Record” constraint says only that the type must be a value type, it says nothing about records with managed fields not being allowed. And there is nothing in the release notes about this change in behavior, either. If this is really “by design”, then it needs to be documented. Otherwise, this clear regression needs to be fixed.

DT Friday 8:02 PM

  • This modification invalidated hundreds of lines of code in the DMVCFramework unit tests and, probably, many more in users’ projects.

  • The criterion used (if there is something “managed” in the record, it is no longer a simple-value-type) is understandable but leaves an obvious gap in the generic constraints mechanism.

    • Does it mean that a record with a string field is no longer a value type? Is it no longer a record (for the generic constraint criteria)?
    • What constraint should I use to allow a generic to be usable only by records (which might have string fields)? I cannot find suitable constraint.
    • Moreover, if a record with a string field behaves like a value type, why shouldn’t the generic constraint consider it as such and should use a more stringent criterion?

Marco Cantu Friday 8:56 PM

  • This restriction will be removed in a 12.3 patch, coming soon.

I got hit with this in my code too - hopefully the patch will turn up soon.

1 Like

The patch is out.

1 Like