Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are GUIDs necessary to use interfaces in Delphi?

The official documentation says they are optional. I know COM interop requires a unique identifier for each interface but every interface example I see has a GUID whether it's used with COM or not? Is there any benefit to including a GUID if its not going to be used with COM?

like image 483
Kenneth Cochran Avatar asked Jun 07 '10 18:06

Kenneth Cochran


People also ask

Why use interface in Delphi?

It makes it easier for the programmer to be disciplined in developing the application; all of the classes have an additional set of methods that are identical. And Delphi insists that all of the methods in an interface are implemented.

What is GUID in Delphi?

GUID – Globally Unique IDentifier In Delphi, GUID values are represented with the TGuid record defined in the System unit. A Guid value is a 128-bit integer (16 bytes) that can be used in database applications when a unique identifier is required.


1 Answers

I've noticed that some methods such as Supports (to determine if a class conforms to a specific interface) require that you define a GUID before you can use them.

This page confirms it with the following information:

Note: The SysUtils unit provides an overloaded function called Supports that returns true or false when class types and instances support a particular interface represented by a GUID. The Supports function is used in the manner of the Delphi is and as operators. The significant difference is that the Supports function can take as the right operand either a GUID or an interface type associated with a GUID, whereas is and as take the name of a type. For more information about is and as, see Class References.

Here's some interesting information about interfaces, which states:

Why does an interface need to be uniquely identifiable? The answer is simple: because Delphi classes can implement multiple interfaces. When an application is running, there has to be a mechanism that will get pointer to an appropriate interface from an implementation. The only way to find out if an object implements an interface and to get a pointer to implementation of that interface is through GUIDs.

Emphasis added in both quotes.

Reading this entire article also makes you realize that QueryInterface (which requires a GUID) is used behind the scenes for reasons such as reference counting.

like image 179
Senseful Avatar answered Oct 15 '22 00:10

Senseful