Forum > General

Good explaination of how interfaces work.

(1/5) > >>

Thaiblood:
Hi
I have looked and looked and i cannot figure out how to use interfaces. I have recently taugh myself the Go programming language and i think i have the gist of what an interface is supposed to do. But non of the examples i try for pascal work. Some of the examples say that i must specify a unique id thing. Why is that neccesary and how do i obtain one for the interfaces that i want to make?

So to sum up my question:

1. Explain to me how to use interfaces in freepascal with an example.
2. Is the unique id thing strictly neccesary and what is it's purpose?
3. If the unique id thing is neccesary, then how do i obtain one for the interfaces i am creating.
4. If you know, then also explain to me the differences from freepascal's interfaces and Golang's interfaces.

Thank
 

dje:

--- Quote from: Thaiblood on October 02, 2022, 04:38:16 am ---1. Explain to me how to use interfaces in freepascal with an example.

--- End quote ---

Google FreePascal interfaces (or Delphi interfaces). There is a ton of information online:
https://wiki.lazarus.freepascal.org/Understanding_Interfaces
https://wiki.lazarus.freepascal.org/How_To_Use_Interfaces


--- Quote from: Thaiblood on October 02, 2022, 04:38:16 am ---2. Is the unique id thing strictly neccesary and what is it's purpose?

--- End quote ---
No. Its for COM programming. "It is used to enable inter-process communication object creation in a large range of programming languages"
https://en.m.wikipedia.org/wiki/Component_Object_Model

--- Quote ---COM programmers build their software using COM-aware components. Different component types are identified by class IDs (CLSIDs), which are Globally Unique Identifiers (GUIDs). Each COM component exposes its functionality through one or more interfaces. The different interfaces supported by a component are distinguished from each other using interface IDs (IIDs), which are GUIDs too. COM interfaces have bindings in several languages, such as C, C++, Visual Basic, Delphi, Python[10][11] and several of the scripting languages implemented on the Windows platform. All access to components is done through the methods of the interfaces. This allows techniques such as inter-process, or even inter-computer programming (the latter using the support of DCOM).
--- End quote ---

--- Quote from: Thaiblood on October 02, 2022, 04:38:16 am ---3. If the unique id thing is neccesary, then how do i obtain one for the interfaces i am creating.

--- End quote ---
Ctrl+Shift+G


--- Quote from: Thaiblood on October 02, 2022, 04:38:16 am ---4. If you know, then also explain to me the differences from freepascal's interfaces and Golang's interfaces.

--- End quote ---
Unsure. It looks like Go doesn't have classes, but uses "interfaces", which seems to be an abstract type. Maybe its similar to C++ pure classes, or Pascals abstract classes.

I wouldn't assume they are the same. In general, interfaces are not 100% needed for general FreePascal programming, so I would keep away from them until you have learn't and exhausted all the other Object Pascal language features.

Mr.Madguy:

--- Quote from: dje on October 02, 2022, 05:16:48 am ---No. Its for COM programming. "It is used to enable inter-process communication object creation in a large range of programming languages"
https://en.m.wikipedia.org/wiki/Component_Object_Model

--- End quote ---
Not necessary. GUID is required for IS and AS operators to work. And these two operators are required for virtual casting to work.

I guess, it's better to read this article.

MarkMLl:

--- Quote from: Mr.Madguy on October 02, 2022, 08:02:31 am ---I guess, it's better to read this article.

--- End quote ---

Well, as a "Classical Pascal" guy I think of myself as being in much the same boat as OP. And every time I try to get to grips with interfaces I come up against something like (from the wiki article cited above):

"You should understand, that there are two kinds of interfaces. ... 1) Interfaces as tool to implement multiple inheritance, as they work in some other languages, like Java 2) OLE/COM interfaces as Windows-specific language-independent way to implement OOP."

But the code example right at the top of that wiki page refers to CORBA, which is neither (1) nor (2). At that point I give up. Again.

It might possibly be safe to say something like "In a component-based system, each underlying component is described by an interface, sometimes with an associated GUID. At the application programming level, multiple interfaces can be grouped together to provide the functionality for an object." But on a system that doesn't have all its functionality in external components, it leaves me wondering what all the fuss is about.

MarkMLl

Mr.Madguy:

--- Quote from: MarkMLl on October 02, 2022, 09:01:54 am ---Well, as a "Classical Pascal" guy I think of myself as being in much the same boat as OP. And every time I try to get to grips with interfaces I come up against something like (from the wiki article cited above):

"You should understand, that there are two kinds of interfaces. ... 1) Interfaces as tool to implement multiple inheritance, as they work in some other languages, like Java 2) OLE/COM interfaces as Windows-specific language-independent way to implement OOP."

But the code example right at the top of that wiki page refers to CORBA, which is neither (1) nor (2). At that point I give up. Again.

It might possibly be safe to say something like "In a component-based system, each underlying component is described by an interface, sometimes with an associated GUID. At the application programming level, multiple interfaces can be grouped together to provide the functionality for an object." But on a system that doesn't have all its functionality in external components, it leaves me wondering what all the fuss is about.

MarkMLl

--- End quote ---
Well, it's actually not my article, so it isn't my example. It's just copy-paste of my post from some other thread about interfaces. Yeah, reference counting is nice thing, but it's not mandatory. One can control object's life time by himself, if he desires to do it.

Inheritance - is confusing thing. It's called inheritance and it's syntax looks like inheritance (TChild = class(TParent)  looks like it's TChild <- TParent), but it's actually two-way process, i.e. TChild <-> TParent. It's both inheritance (child inherits something from parent) and polymorphism (child implements some parent's features). And there is one thing about inheritance. While multiple inheritance is considered to be bad thing, multiple polymorphism - isn't. Interfaces are just some way to implement "lite" version of multiple polymorphism, i.e. to implement multiple polymorphism without actually implementing multiple inheritance.

And multiple polymorphism is needed, when objects should behave differently in different situations. For example TList can be both TCountable and TEnumerable. It's very powerful tool, as it removes restrictions of static typification.

P.S. My way to interfaces was long and hard. As always, one starts to understand, for what purpose certain tools are needed, only when he faces so complex task, that is very hard to implement without such tools. My task was - to reuse TChild's code instead of copy-pasting it to TModifiedChild:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TParent = class;abstract; TChild = class(TParent); TModifiedParent = class(TParent);abstract; TModifiedChild = class(TChild, TModifiedParent); 

Navigation

[0] Message Index

[#] Next page

Go to full version