"Class Modules" (Create Instances of a Module)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

"Class Modules" (Create Instances of a Module)

Post by c4s »

Context: Lunasole's post inspired me to create a separate thread so that his great suggestion doesn't get buried:
Lunasole wrote:[...] In other words, those classes in VB6 are exactly just like modules in PB, but they also allow to dynamically create and destroy multiple instances of them each separated from others.
It's funny because when "basic OOP" was first mentioned I had the exact same idea in mind: Why not simply allow multiple instances of a module? (When I first looked at PB's modules I was kind of bummed because I actually expected it. It still feels strange not being able to use a separate instance.)

I just tried to think of a PB-close syntax for this exiting new feature (only one new keyword and function!):

Code: Select all

; It's surprisingly similar to existing module concepts, the handling of lists/arrays/maps and structured variables:

; 0. Regular declaration and implemenation of module "MyModule"
; [...]

; 1. Definition and instantiation
Define.MyModule NewModule module1, NewModule module2

; 2. As soon as NewModule is called the module's code gets executed (separately) to be able to simulate a constructor call

; 3.1. Accessing a particular module instance
module1::SetString("abc")
module2::SetString("def")

; 3.2. Some syntactical sugar
With module1
	Debug ::GetString()  ; Outputs "abc" (not "def"), great!
EndWith

; 4. Destruction
FreeModule(module1, @MyModule::MyDestroy())
FreeModule(module2)  ; Automatically calls MyModule::Destroy() of that instance


; Of course known usage works as before:

; Alternative A: As another instance
MyModule::SetString("xxx")
Debug MyModule::GetString()  ; Outputs "xxx"
Debug module1::GetString()  ; Still outputs "abc"

; Alternative B: As some kind of "static module" (more intuitive?)
MyModule::SetString("xxx")
Debug MyModule::GetString()  ; Outputs "xxx"
Debug module1::GetString()  ; Outputs "xxx" as well (like module2)
I think Lunasole's idea and my proposed syntax is quite neat and perfectly fits in the existing world of PureBasic. What do you think?

(And please, because I know it often leads to a heated debate, let's keep this topic focused on this particular concept/suggestion of "class modules". General thoughts on object-orientated programming can be posted elsewhere. Thanks.)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: "Class Modules" (Create Instances of a Module)

Post by User_Russian »

http://www.purebasic.fr/english/viewtop ... 95#p454795
Can make a class, very similar to module.

Code: Select all

DeclareClass Test
  ; Public elements.
  Global x.l
  Global y.l
  Declare Plus()

EndDeclareClass


Class Test
  ; Private elements.
  Global z.l
  
  Procedure Plus()
    Result = x + y
    z + Result
    ProcedureReturn Result
  EndProcedure

EndClass



x.Test ; Create an instance of class.
x\x=2
x\y=8
Debug x\Plus() ; Return value 10.
Post Reply