Lazarus

Free Pascal => General => Topic started by: vfclists on March 06, 2016, 12:03:31 am

Title: A few questions about Interfaces : Part 2
Post by: vfclists on March 06, 2016, 12:03:31 am
What is the role of the GUID in interfaces? Given their uniqeuness does that mean that once you compile an interface you shouldn't change the interface definition again?

Is the GUID actually necessary and is it only required on Windows?
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 06:23:44 am
GUID is the interface's ID with out a GUID and interface can't be identified at all. It is there to avoid using or embosing a language's internals to the others, and it is used to find or created an object that implements the interface . Corba interfaces have a plain string as Id instead of a GUID which can have what ever form you like but I do prefer the GUID idea and probably I will use it on corba interfaces too.
Title: Re: A few questions about Interfaces : Part 2
Post by: Basile-B on March 06, 2016, 06:38:05 am
with CORBA interfaces, the GUID is completly optional since you'll never use it, in this case you just extract an interface with as and the type.
It's not like with QueryInterface() and COM that need to work in an language-agnostic fashion.
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 06:45:46 am
with CORBA interfaces, the GUID is completly optional since you'll never use it, in this case you just extract an interface with as and the type.
It's not like with QueryInterface() and COM that need to work in an language-agnostic fashion.
Does that mean that corba interfaces are not language agnostic? in sort I can't have interface implemented in other languages than the one the consumer is written in? Sorry but my corba interface knowledge is minimal.
Title: Re: A few questions about Interfaces : Part 2
Post by: Thaddy on March 06, 2016, 08:32:05 am
Does that mean that corba interfaces are not language agnostic? in sort I can't have interface implemented in other languages than the one the consumer is written in? Sorry but my corba interface knowledge is minimal.

No. just like with COM interfaces there is an IDL description that is language agnostic. If anything CORBA is more language agnostic and platform agnostic than COM.
There a good write up in wikipedia:
https://en.wikipedia.org/wiki/Common_Object_Request_Broker_Architecture
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 08:42:34 am
I still do not understand why this GUID.
I do understand interfaces as described in article which vfclist links to in his other topic - http://www.delphibasics.co.uk/Article.asp?Name=Interface (http://www.delphibasics.co.uk/Article.asp?Name=Interface)
I do understand well that this is very useful.

Note that in that article guids are not used!

And then this two posts are more confusing:
with CORBA interfaces, the GUID is completly optional since you'll never use it, in this case you just extract an interface with as and the type.
It's not like with QueryInterface() and COM that need to work in an language-agnostic fashion.
Does that mean that corba interfaces are not language agnostic? in sort I can't have interface implemented in other languages than the one the consumer is written in? Sorry but my corba interface knowledge is minimal.

Before Object Pascal, I learned about interfaces in Java (with Java I learned object oriented programming). In Java they are much more used then in Object pascal. But I have NEVER seen GUIDs in Java.

What do you mean by "language agnostic"? Are you talking about using interfaces declared in pascal in other languages? How? In which languages? C++, Java? And why? Why would you need interfaces from one language to use in another?


Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 09:06:36 am
I still do not understand why this GUID.
I do understand interfaces as described in article which vfclist links to in his other topic - http://www.delphibasics.co.uk/Article.asp?Name=Interface (http://www.delphibasics.co.uk/Article.asp?Name=Interface)
I do understand well that this is very useful.

Note that in that article guids are not used!

And then this two posts are more confusing:
with CORBA interfaces, the GUID is completly optional since you'll never use it, in this case you just extract an interface with as and the type.
It's not like with QueryInterface() and COM that need to work in an language-agnostic fashion.
Does that mean that corba interfaces are not language agnostic? in sort I can't have interface implemented in other languages than the one the consumer is written in? Sorry but my corba interface knowledge is minimal.

Before Object Pascal, I learned about interfaces in Java (with Java I learned object oriented programming). In Java they are much more used then in Object pascal. But I have NEVER seen GUIDs in Java.

What do you mean by "language agnostic"? Are you talking about using interfaces declared in pascal in other languages? How? In which languages? C++, Java? And why? Why would you need interfaces from one language to use in another?
Yes when I say language agnostic I do mean that in interfaces defined in C++ can be implemented in pascal or basic and used from C++ basic, python, java, or any languages that has library to support them. It is the most usable feature of interfaces it is the feature that used to allow activeX controls to be used from any language including pascal, java etc. A framework build as language agnostic will have to follow very tight specifications it is the same as you writing a dll using only C data types but object oriented. 
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 10:58:55 am
All tutorials about interfaces talk about them like abstract classes, from which then different classes can inherit common functionality.
But, if I understand correctly, apart from this, interfaces have another completely different purpose?

I still do not understand much about using it in different languages and I am not very interested in that now... but, are these GUIDs actually only for that?
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 11:06:44 am
All tutorials about interfaces talk about them like abstract classes, from which then different classes can inherit common functionality.
But, if I understand correctly, apart from this, interfaces have another completely different purpose?
In pascal interfaces is the only working mechanism for "multiple inheritance" as well

I still do not understand much about using it in different languages and I am not very interested in that now... but, are these GUIDs actually only for that?
For what exactly? As far as I know the role of an ID is to uniquely identify an interface. GUIDs were created in order to make sure that each ID was globally unique avoiding using a registry and all the problems that come with it.
Title: Re: A few questions about Interfaces : Part 2
Post by: Thaddy on March 06, 2016, 11:12:16 am
Well, look at it this way:
Both in CORBA as in COM there is an intermediate Interface Description Language (IDL) that describes how to translate what is described (in IDL) into a native language like C++ or Object Pascal.

Both Delphi and Freepascal hide that intermediate step for you. That may put you on the wrong foot.

If you really want to use CORBA and not a seasoned programmer forget about GUID's altogether.
And forget about ActiveX supporting CORBA. It doesn't. ActiveX supports COM, which NEEDS GUID's (most of the time) and works completely different. And you didn't read the wiki entry I posted here...
If you want to understand, read references, although asking questions might help. But do something with the answers.
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 11:14:46 am
As far as I know the role of an ID is to uniquely identify an interface. GUIDs were created in order to make sure that each ID was globally unique avoiding using a registry and all the problems that come with it.

Sorry, I am not following... What ID? What identifying? Without ID, what would you use a registry for?

See the article with which we started "The reason for interfaces" (http://www.delphibasics.co.uk/Article.asp?Name=Interface) - no IDs, no GUIDs. Also, in Java tutorial, section about interfaces (https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html), again no IDs, no GUIDs.
And I do not see problem with identifying interface
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 11:23:41 am
As far as I know the role of an ID is to uniquely identify an interface. GUIDs were created in order to make sure that each ID was globally unique avoiding using a registry and all the problems that come with it.

Sorry, I am not following... What ID? What identifying? Without ID, what would you use a registry for?

See the article with which we started "The reason for interfaces" (http://www.delphibasics.co.uk/Article.asp?Name=Interface) - no IDs, no GUIDs. Also, in Java tutorial, section about interfaces (https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html), again no IDs, no GUIDs.
And I do not see problem with identifying interface
So how do you propose to ask for a specific interface from a factory for instance?Please keep in mind that you do not write the factory nor have access to its code.
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 11:26:38 am
So how do you propose to ask for a specific interface from a factory for instance?

What factory?

I am sorry, I just do not follow. :-[
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 11:30:19 am
Please keep in mind that you do not write the factory nor have access to its code.

Okay, I guess there is much more about interfaces that is not covered in mentioned tutorials. Sorry, thank you for trying to answer.
This is obviously some different matter from just what is described in those articles.
Title: Re: A few questions about Interfaces : Part 2
Post by: Thaddy on March 06, 2016, 11:44:18 am
As far as I know the role of an ID is to uniquely identify an interface. GUIDs were created in order to make sure that each ID was globally unique avoiding using a registry and all the problems that come with it.

Sorry, I am not following... What ID? What identifying? Without ID, what would you use a registry for?

See the article with which we started "The reason for interfaces" (http://www.delphibasics.co.uk/Article.asp?Name=Interface) - no IDs, no GUIDs. Also, in Java tutorial, section about interfaces (https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html), again no IDs, no GUIDs.
And I do not see problem with identifying interface
Ah I see. you are not familiar with the meaning of the English language word "interface" which simply means a way to communicate.
See for a more specific explanation regarding computing https://en.wikipedia.org/wiki/Interface_%28computing%29.
Point is: in java it has again different semantics from CORBA or even more different from COM.
Three ways to solve a problem. So basically, you are confused because not every type of interface is the same interface type.
It is not really that hard.
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 02:01:26 pm
Ah I see. you are not familiar with the meaning of the English language word "interface" which simply means a way to communicate.
See for a more specific explanation regarding computing https://en.wikipedia.org/wiki/Interface_%28computing%29.
Point is: in java it has again different semantics from CORBA or even more different from COM.
Three ways to solve a problem. So basically, you are confused because not every type of interface is the same interface type.
It is not really that hard.

Thanks.
I do know the English word, it is the language feature which I think is not described well. My point is that what is written here - http://www.delphibasics.co.uk/Article.asp?Name=Interface (http://www.delphibasics.co.uk/Article.asp?Name=Interface) and also the usage described in fpc reference manual - http://www.freepascal.org/docs-html/current/ref/refch7.html (http://www.freepascal.org/docs-html/current/ref/refch7.html) is the same usage as in Java (https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html).
These interfaces, without any attempt to "interface other languages" are very much used in Java. And this has nothing to do with GUIDs, nothing to do with identification and "factories" tazz is talking about.

Sorry, I still don't understand it well, but thank you very much for trying.
Title: Re: A few questions about Interfaces : Part 2
Post by: marcov on March 06, 2016, 02:17:12 pm
See also http://stackoverflow.com/questions/9592654/what-are-the-differences-between-implementation-of-interfaces-in-delphi-and-laza/9623502#9623502

And getting an object from a Corba broker is also quite complicated. It just doesn't need much compiler assistance, which is why Corba interfaces are so thin.
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 03:25:51 pm
Ah I see. you are not familiar with the meaning of the English language word "interface" which simply means a way to communicate.
See for a more specific explanation regarding computing https://en.wikipedia.org/wiki/Interface_%28computing%29 (https://en.wikipedia.org/wiki/Interface_%28computing%29).
Point is: in java it has again different semantics from CORBA or even more different from COM.
Three ways to solve a problem. So basically, you are confused because not every type of interface is the same interface type.
It is not really that hard.

Thanks.
I do know the English word, it is the language feature which I think is not described well. My point is that what is written here - http://www.delphibasics.co.uk/Article.asp?Name=Interface (http://www.delphibasics.co.uk/Article.asp?Name=Interface) and also the usage described in fpc reference manual - http://www.freepascal.org/docs-html/current/ref/refch7.html (http://www.freepascal.org/docs-html/current/ref/refch7.html) is the same usage as in Java (https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html).
These interfaces, without any attempt to "interface other languages" are very much used in Java. And this has nothing to do with GUIDs, nothing to do with identification and "factories" tazz is talking about.

Sorry, I still don't understand it well, but thank you very much for trying.
Ok let me try to give an example that is closer to pascal object hierarchy. Have you ever used the is operator? EG
Code: Delphi  [Select]
  1. if Sender is Tbutton then ....;
Do you understand what "is" checks when called? Now ask your self, if you pass a TButton that is created in a DLL why "is" fails to correctly identify the same class?
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 06, 2016, 05:39:56 pm
Ok let me try to give an example that is closer to pascal object hierarchy. Have you ever used the is operator? EG
Code: Delphi  [Select]
  1. if Sender is Tbutton then ....;
Do you understand what "is" checks when called? Now ask your self, if you pass a TButton that is created in a DLL why "is" fails to correctly identify the same class?

I guess that this can be explained that dll has its own copy of lcl hierarhy and that is the reason that TButton from exe and TButton from dll are seen as two different classes?
All the time I am trying to figure out what it has to do in normal usage, classes from dlls are not the cases I'm talking about. Neither are interfaces, as normal language feature.
But we see that this problem is present with classes as well.

So... I'm just beginning to realize that you use interfaces from dlls (that is what you were talking about when you were talking about other programming languages, isn't that?).

Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 06, 2016, 06:07:57 pm
Ok let me try to give an example that is closer to pascal object hierarchy. Have you ever used the is operator? EG
Code: Delphi  [Select]
  1. if Sender is Tbutton then ....;
Do you understand what "is" checks when called? Now ask your self, if you pass a TButton that is created in a DLL why "is" fails to correctly identify the same class?

I guess that this can be explained that dll has its own copy of lcl hierarhy and that is the reason that TButton from exe and TButton from dll are seen as two different classes?
Which makes the memory address the ID of the class. Now invent an other ID that will be able to work across processes, executables and libraries after that make sure that there is no conflict between the IDs and then extend it to multiple companies implementing potentially the same interface and you start getting the idea behind the GUIDs.

All the time I am trying to figure out what it has to do in normal usage, classes from dlls are not the cases I'm talking about. Neither are interfaces, as normal language feature.
But we see that this problem is present with classes as well.
Well, pascal units already have an interface part that plays the role of interfaces in other languages the implementation and definition of the class are already separated there is no need for an interface type as well. Interface types in pascal by definition are there to support object implementations in other languages and in extension from DLLs.  With out the language agnostic part of the interfaces, they provide nothing that an abstract class doesn't already provide.
Title: Re: A few questions about Interfaces : Part 2
Post by: vfclists on March 07, 2016, 08:57:32 am
Concerning the GUID I take it that once a GUID is allocated and committed to some kind of registry, it is not meant to be changed again, ie implementation details might change, but the function signatures should not change again.

But in the case of your own locally developed code can you change the interface whilst retaining the same GUID  so long as all the dependent code is available to be recompiled?
Title: Re: A few questions about Interfaces : Part 2
Post by: Zoran on March 07, 2016, 11:16:50 am
With out the language agnostic part of the interfaces, they provide nothing that an abstract class doesn't already provide.

Oh, no, with this I dissagree, they do!
They provide a way to define common behaviour for classes which do not have common superclass. It is a very useful feature, as multiple inheritance does not exist in Object Pascal and Java. The link which vfclists gave (again, this one) (http://www.delphibasics.co.uk/Article.asp?Name=Interface) describes that (see the first section - reason for interfaces).

In Java it is very much used, all over the standard java class libraries. In FPC and Delphi it is not used that much, I don't know why (a bright exception is ZeosLib - many functionalities are implemented with interfaces, I believe it is because Zeos is highly influenced by Java's JDBC library and they used their filosofy).

But in the case of your own locally developed code can you change the interface whilst retaining the same GUID  so long as all the dependent code is available to be recompiled?

As far as I understand now, the GUID-s matter only for exporting them in dynamic libraries/shared objects. When used as a normal language feature (see above), you just don't need them.
Title: Re: A few questions about Interfaces : Part 2
Post by: marcov on March 07, 2016, 11:38:23 am
Concerning the GUID I take it that once a GUID is allocated and committed to some kind of registry, it is not meant to be changed again, ie implementation details might change, but the function signatures should not change again.

If you use it with COM yes. If you use it without COM, it is not in a registry, but the functioning of the IS operator might depend on its uniqueness (within the binary).

Quote
But in the case of your own locally developed code can you change the interface whilst retaining the same GUID  so long as all the dependent code is available to be recompiled?

In the case of non COM: all no problem.  In case of COM (Tautoobject?), one registers an interface using an implementation (location of OCX/DLL). In such case if the existing registration is for a different dll (path) as the current one, the old stuff might be used and found.
Title: Re: A few questions about Interfaces : Part 2
Post by: taazz on March 07, 2016, 11:45:56 am
With out the language agnostic part of the interfaces, they provide nothing that an abstract class doesn't already provide.

Oh, no, with this I dissagree, they do!
They provide a way to define common behaviour for classes which do not have common superclass. It is a very useful feature, as multiple inheritance does not exist in Object Pascal and Java. The link which vfclists gave (again, this one) (http://www.delphibasics.co.uk/Article.asp?Name=Interface) describes that (see the first section - reason for interfaces).

In Java it is very much used, all over the standard java class libraries. In FPC and Delphi it is not used that much, I don't know why (a bright exception is ZeosLib - many functionalities are implemented with interfaces, I believe it is because Zeos is highly influenced by Java's JDBC library and they used their filosofy).
Good, I was wandering how long it will take to spot it. Yes multiple inheritance is  the only positive of using interfaces but it requires a shift in code paradigm for proper use. For example you shouldn't use object variables at all the auto ref count will destroy interfaces and leave a bunch of variable pointing to invalid memory addresses. Using corba interfaces is a middle road but after a while you will probably want to use them anyway.
But in the case of your own locally developed code can you change the interface whilst retaining the same GUID  so long as all the dependent code is available to be recompiled?

As far as I understand now, the GUID-s matter only for exporting them in dynamic libraries/shared objects. When used as a normal language feature (see above), you just don't need them.
Wrong guids are the IDs of an interface the same way the class memory address is the ID of a class, with out the GUIDs you are left with unsafe casting to put it on class metaphor, on top of that supports does not work with out a GUID I think both in corba and COM interfaces but that is an assumption I haven't really tested.
Title: Re: A few questions about Interfaces : Part 2
Post by: marcov on March 07, 2016, 11:51:13 am
In Java it is very much used, all over the standard java class libraries. In FPC and Delphi it is not used that much, I don't know why (a bright exception is ZeosLib - many functionalities are implemented with interfaces, I believe it is because Zeos is highly influenced by Java's JDBC library and they used their filosofy).

I think there are two main reasons:

- interfaces only came later, when the base RTL and VCL already existed. Therefore TPersistent (the abstract "storage"is a class, not an interface)
- In Java memory management is the same for both, and one can use objects and interfaces mixed. In Delphi one can't.  (only recounted COM interfaces)