The ability to Create non-visual Classes
The ability to Create non-visual Classes
Hi
I would like to request the ability to define classes and create instances. I am not asking for everything to be OOP but just the ability to define non-visual classes with properties, methods, events etc. and use instances of those classes in my code.
Thanks,
Simon
I would like to request the ability to define classes and create instances. I am not asking for everything to be OOP but just the ability to define non-visual classes with properties, methods, events etc. and use instances of those classes in my code.
Thanks,
Simon
Simon White
dCipher Computing
dCipher Computing
Re: The ability to Create non-visual Classes
Code: Select all
Prototype P_MyClass_Shout(*Self, Msg.s)
Structure MyClass
Name.s
Shout.P_MyClass_Shout
EndStructure
Procedure MyClass_Shout(*Self.MyClass, Msg.s)
MessageRequester("Shout!", Msg + " from " + *Self\Name + "!")
EndProcedure
Procedure MyClass()
*Self.MyClass = AllocateMemory(SizeOf(MyClass))
InitializeStructure(*Self, MyClass)
*Self\Shout = @MyClass_Shout()
ProcedureReturn *Self
EndProcedure
*Bill.MyClass = MyClass()
*Bill\Name = "Bill"
*Bob.MyClass = MyClass()
*Bob\Name = "Bob"
*Bill\Shout(*Bill, "Hello world!")
*Bob\Shout(*Bob, "Goodbye, world!")
Re: The ability to Create non-visual Classes
The address of a particular object's method can't be obtained for callbacks. Sure, you can get a "general" procedure address and pass the object around. But that really defeats the purpose of encapsulation.
Always having to pass "*Self" is also annoying.
Code: Select all
Prototype P_MyClass_Shout(*Self, Msg.s)
Structure MyClass
Name.s
Shout.P_MyClass_Shout
EndStructure
Procedure MyClass_Shout(*Self.MyClass, Msg.s)
MessageRequester("Shout!", Msg + " from " + *Self\Name + "!")
EndProcedure
Procedure MyClass()
*Self.MyClass = AllocateMemory(SizeOf(MyClass))
InitializeStructure(*Self, MyClass)
*Self\Shout = @MyClass_Shout()
ProcedureReturn *Self
EndProcedure
*Bill.MyClass = MyClass()
*Bill\Name = "Bill"
*Bob.MyClass = MyClass()
*Bob\Name = "Bob"
; *Bill\Shout(*Bill, "Hello world!")
; *Bob\Shout(*Bob, "Goodbye, world!")
Debug @MyClass_Shout()
Debug @*Bill\Shout()
Re: The ability to Create non-visual Classes
Huh?!?Mistrel wrote:The address of a particular object's method can't be obtained for callbacks. Sure, you can get a "general" procedure address and pass the object around. But that really defeats the purpose of encapsulation.
Code: Select all
Debug @MyClass_Shout()
Debug *Bill\Shout
Re: The ability to Create non-visual Classes
My debug window says:
The address result from both cases is the general class procedure (similar to a static method) instead of the object's method, as one might expect in OO. You could pass the object instead of a callback and further increase ambiguity.
For example, you can't mix OO methods where a procedural function is expected since you can only pass the object itself. Because there is no support for interfaces or abstract classes or type checking to enforce it, it's impossible to guarantee specific functionality.
If PureBasic were to support this it would first need to support some way of defining a class interface for inheritance. And because there is no type checking for pointers, the next logical step would be to add pass-by-reference so that an object's type can be checked like any other type. This would avoid having to type pointers.
And for those who don't know and might be quick to reply, the "Interface" keyword in PureBasic is not the same as a class interface for use with inheritance.
Code: Select all
4199281
4199281
For example, you can't mix OO methods where a procedural function is expected since you can only pass the object itself. Because there is no support for interfaces or abstract classes or type checking to enforce it, it's impossible to guarantee specific functionality.
If PureBasic were to support this it would first need to support some way of defining a class interface for inheritance. And because there is no type checking for pointers, the next logical step would be to add pass-by-reference so that an object's type can be checked like any other type. This would avoid having to type pointers.
And for those who don't know and might be quick to reply, the "Interface" keyword in PureBasic is not the same as a class interface for use with inheritance.
Re: The ability to Create non-visual Classes
Huh? The object's method is returned, because it's the same as the static method. If you override the method in a specific instance, or in a descended class, it will return the new address.Mistrel wrote:My debug window says:
The address result from both cases is the general class procedure (similar to a static method) instead of the object's method, as one might expect in OO.Code: Select all
4199281 4199281
This works the same as in C++, except in C++ you can't get to the actual address ("ISO C++ forbids taking the address of a bound member function to form a pointer to member function.")
Re: The ability to Create non-visual Classes
You're right. My mistake. 
I do support this request, however. Defining visual classes would be very helpful.

I do support this request, however. Defining visual classes would be very helpful.
Re: The ability to Create non-visual Classes
Hi
Yes visual classes would be nice but they would I assume be a lot more work. In my case most of my front ends are are or will be Web Browser based so for me just having the ability to define classes that do not require any visual representation is fine and I think would be a great feature for an already great product. The speed of PB is allowing me to develop what amounts to an application server for web browser applications.
The ability to use procedural code and classes is a nice combination. I used it a lot in a previous development environment that supported both programming methods.
Simon
Yes visual classes would be nice but they would I assume be a lot more work. In my case most of my front ends are are or will be Web Browser based so for me just having the ability to define classes that do not require any visual representation is fine and I think would be a great feature for an already great product. The speed of PB is allowing me to develop what amounts to an application server for web browser applications.
The ability to use procedural code and classes is a nice combination. I used it a lot in a previous development environment that supported both programming methods.
Simon
Simon White
dCipher Computing
dCipher Computing
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: The ability to Create non-visual Classes
Interfaces are cleaner than prototypes and with a virtual table in a datasection there is no need to pass a *self pointer. Take a look at srod's tutorial on nxsoftware.com "using OOP in Purebasic", he does a good job of explaining the approach.
BERESHEIT
Re: The ability to Create non-visual Classes
Thanks for the link the article is very helpful.netmaestro wrote:Interfaces are cleaner than prototypes and with a virtual table in a datasection there is no need to pass a *self pointer. Take a look at srod's tutorial on nxsoftware.com "using OOP in Purebasic", he does a good job of explaining the approach.
Simon
Simon White
dCipher Computing
dCipher Computing
Re: The ability to Create non-visual Classes
Agreed! Prototypes aren't really suitable because of passing *self all the time. Interfaces are cleaner.netmaestro wrote:Interfaces are cleaner than prototypes and with a virtual table in a datasection there is no need to pass a *self pointer.
Still, it would be nice to have fully supported OOP integrated in PB for serious work.
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book. -- K&R (2nd Ed.) : Page 65
Re: The ability to Create non-visual Classes
Where ever the vtable points to, you never need to pass the *self pointer with Interfaces.netmaestro wrote:Interfaces are cleaner than prototypes and with a virtual table in a datasection there is no need to pass a *self pointer. Take a look at srod's tutorial on nxsoftware.com "using OOP in Purebasic", he does a good job of explaining the approach.

Prototypes are for better handling of DLLs, not to replace Interfaces.
MFG PMV
Re: The ability to Create non-visual Classes
Interfaces make it difficult to allow instances to override methods.
Re: The ability to Create non-visual Classes
I often use the following pattern to avoid overridding methods:Trond wrote:Interfaces make it difficult to allow instances to override methods.
This allows me to put the default method code in myMethodExec and if I want to override it I simply put my code in myMethodPre() and return 0. This pattern creates a shallow inheritance hierarchy and greatly reduces the number of classes I need.Interface
myMethod
EndInterface
Procedure myMethod
If myMethodPre()
myMethodExec()
EndIf
myMethodPost()
Simon White
dCipher Computing
dCipher Computing
Re: The ability to Create non-visual Classes
The vtable points to a list of pointers to the procedurs. If you want to change theTrond wrote:Interfaces make it difficult to allow instances to override methods.
call of one procedure, you only need to override this single pointer. That is the same,
if you want to override the pointer of a prototype.
MFG PMV