Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What "type of" declaration represents in Delphi and how can it be used

Tags:

There is some strange code in Datasnap.DSReflect unit

  TDSAdapterClassType = type of TDSAdapterClass;    TDSAdapterClass = class(TPersistent)   private     FAdapteeInstance: TObject;   public     constructor Create(AdapteeInstance: TObject); virtual;   end; 

and then it is used like this

var   AdapteeInstance: TObject;   FClassRef: TPersistentClass;    Result := TDSAdapterClassType(FClassRef).Create(AdapteeInstance); 

At first sight it seems just like another way of declaring class reference. But logic implies that it makes no sense to introduce such variation of language construct without adding more functionality to it. Following that logic I discovered that following declarations compile:

type   TypeOfInteger = type of Integer;   TypeOfByte = type of Byte;    TRec = record     x: integer;   end;   TypeOfTRec = type of TRec;    TBytes = array of byte;   TypeOfTBytes = type of TBytes; 

Interesting enough, following declaration cannot be compiled.

type   TypeOfString = type of String; 

So the question is what type of actually represents and how can it be used in real life application, besides being some kind of alias for class of

Note: type of does not compile in Delphi 7, it seems that it is introduced later on, it is definitively there in XE, but I don't have Delphi 2007-2010 installed to try it there.

Update: I have filled bug report https://quality.embarcadero.com/browse/RSP-9850

like image 830
Dalija Prasnikar Avatar asked Dec 18 '14 20:12

Dalija Prasnikar


People also ask

What is a case statement in Delphi?

The case statement selects one branch out of many possible branches, depending on the value of an ordinal-type expression. Delphi's case statement extends that of standard Pascal by including the optional else condition. If no other case matches the expression, Delphi executes the else statements.

What is an identifier in Delphi?

A declaration defines an identifier (such as the name of a function or variable) that can be used in expressions and statements, and, where appropriate, allocates memory for the identifier. This topic introduces the Delphi language character set, and describes the syntax for declaring: Identifiers.

What is Delphi structure?

Instances of a structured type hold more than one value. Structured types include sets, arrays, records, and files as well as class, class-reference, and interface types.


2 Answers

In Delphi.Net we have the following definitions in SysUtils:

type   TInterfaceRef = type of interface;    function Supports(const Instance: TObject; const IID: TInterfaceRef): Boolean; overload; inline; 

So it was some kind of replacement for class of that can be used for interface types.

The following document mentions a "Type reference syntax (type of Interface)": http://edn.embarcadero.com/article/29780

Here's some more info: http://hallvards.blogspot.de/2004/11/object-to-interface-casts.html

like image 155
Sebastian Z Avatar answered Oct 02 '22 22:10

Sebastian Z


It seems to be related to PTypeInfo based in the TypeKind as you can write this:

program Project1;  {$APPTYPE CONSOLE}  uses   SysUtils;  type   TIntType = type of Integer;   TInt64Type = type of Int64;  var   intType: TIntType;   int64Type: TInt64Type; begin   try     intType := Integer;     Assert(Pointer(intType) = TypeInfo(Integer));     intType := Cardinal;     Assert(Pointer(intType) = TypeInfo(Cardinal));     intType := NativeInt;     Assert(Pointer(intType) = TypeInfo(NativeInt));     int64Type := Int64;     Assert(Pointer(int64Type) = TypeInfo(Int64));     int64Type := UInt64;     Assert(Pointer(int64Type) = TypeInfo(UInt64));   except     on E: Exception do       Writeln(E.ClassName, ': ', E.Message);   end;   Readln; end. 

But it does not work properly with all types and throws internal compiler errors for some.

like image 35
Stefan Glienke Avatar answered Oct 02 '22 22:10

Stefan Glienke