Classes in PureBasic

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Classes in PureBasic

Post by Fred »

So we have to listen to you ? Not everybody wants OOP, and PB won't have a OOP built-in, it's a design choice and I have made it clear a lot of time on this forum. If you don't like it, do as Shield did, find a tool which fits your needs better.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Classes in PureBasic

Post by skywalk »

Fred, would you consider another language spinoff like PooBasic? :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Classes in PureBasic

Post by Fred »

PooBasic, really ??????? :twisted:
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Classes in PureBasic

Post by skywalk »

I was half kidding. :wink:
The other half was serious. I want to import C++ libs without extern C wrappers. :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Classes in PureBasic

Post by Fred »

It should may be possible trough special interfaces, so it doesn't requiers built-in OOP.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Classes in PureBasic

Post by skywalk »

Ah, that would be awesome. :shock:
The list of C++ libs are growing and not always support extern C.

Forgive my ignorance of features that are OOP limited.
Is the lack of operator and procedure overloading OOP related also?
It is a nuisance to write multiple copies of Procedures just to handle different datatypes.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Classes in PureBasic

Post by freak »

Importing C libs is unlikely in my opinion. The reason is that the C++ standard does not formalize the implementation details of the language so different compilers do important things very differently (name mangling, calling conventions etc.). Trying to handle all that correctly (even if we would support just one C++ compiler per platform) would be much more work than it is worth in my opinion.
quidquid Latine dictum sit altum videtur
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Classes in PureBasic

Post by User_Russian »

Fred wrote:Not everybody wants OOP
Because it is not in PB! :( In this forum, a lot of requests add OOP. :oops:
Really there is not much of tasks for which OOP, most optimal choice. But such tasks have to solve them and the easiest way using OOP.
For example, there are two classes that have the same interface, but the functionality is different. In the procedure of passing a pointer to an object and call methods of the object. Not known in advance what the object class and it does not matter, because the interface is the same. Without OOP do quite complicated (in Example 2 class, but can be greater than 1000).
I can not understand the reason why you refuse to add OOP in PB. Perhaps there is some difficulty in adding (have to rewrite the compiler)?
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Classes in PureBasic

Post by idle »

Fred wrote:PooBasic, really ??????? :twisted:
Think it's already been done, real-y basic :lol:

[stoking the fire] :twisted:

but concerning classes the only thing that'd really make it easier would be if the compiler could set vtable entries.
I always feel like I'm repeating myself setting them.

for UserRussian, maybe this will help

Code: Select all


Macro DefineClass(name)  
  Structure name
   *vt 
EndMacro 

Macro ClassInterface(name)
 EndStructure 
 Interface i#name
   free()
 EndMacro    

Macro SetMethod(address)
  PokeI(*this\vt+MethodOffset,address)
  MethodOffset+SizeOf(Integer)
EndMacro
 
Macro New(Class,Interfaces)
  Procedure New_#Class#() 
    Protected MethodOffset, *this.class 
    *this = AllocateMemory(SizeOf(class))
    *this\vt = AllocateMemory(SizeOf(Interfaces))
    InitializeStructure(*this,class)
EndMacro 

Macro EndClassInterface(name)
  EndInterface
EndMacro 

Macro Methods(name)
  Procedure name#_Free(*this.name) 
  If *this 
    FreeMemory(*this\vt)
    ClearStructure(*this,name)
    FreeMemory(*this)
  EndIf 
 EndProcedure
  
 new(name,i#name) 
 SetMethod(@name#_Free())
 
EndMacro   

Macro EndDefineClass(name)
  ProcedureReturn *this
 EndProcedure 
EndMacro 

;-Test the class macros    
;1) DefineClass(ClassName)
;2) Add Structure fields eg a.i, list alist.i()
;3) ClassInterface(ClassName)
;4) Define the interface methods 
;5) EndClassInterface(ClassName)
;6) Define the class procedures 
;7) Methods(ClassName)
;8) SetMethod(@Of_Each_Method_In_The_Same_Order_As_The_Interface())
;9) EndDefineClass(ClassName)

DefineClass(Test)      
  x.i   ;structure fields
  
  ClassInterface(Test)               
    Plus(*that.Test)         ;interface member    
    Minus(*that.Test)
    Set(x.i)
    Get()
  EndClassInterface(Test) 
  
  Procedure Test_Set(*this.Test,x.i)   ;do the procedures 
    *this\x = x
  EndProcedure 
  
  Procedure Test_Get(*this.Test)
    ProcedureReturn *this\x
  EndProcedure   
  
  Procedure Test_Plus(*this.test,*that.test)
  *this\x + *that\x
  EndProcedure

  Procedure Test_Minus(*this.test,*that.test)
  *this\x - *that\x
  EndProcedure
  
  Methods(Test)   ;define the methods                 
  
  SetMethod(@Test_Plus())   ;set the function pointers in the vtable
  SetMethod(@Test_Minus()) 
  SetMethod(@Test_Set())   
  SetMethod(@Test_Get()) 
  
EndDefineClass(Test) ;end the class        
  

*a.iTest = New_Test() 
*b.iTest = New_Test() 

*a\Set(2)
*b\set(2) 

*a\plus(*b)
Debug *a\get() 

*a\minus(*b) 
Debug *a\get() 

*a\free()
*b\free() 


Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Classes in PureBasic

Post by Shield »

There are OOP pre-compilers available that probably do a better job than macros could,
so these might be an option for User_Russian.

Another approach would be using modules, structures and prototypes,
but that has an equally bad typing overhead, though it doesn't require extensive use of macros
or even a pre-compiler.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Classes in PureBasic

Post by Comtois »

skywalk wrote:Fred, would you consider another language spinoff like PooBasic? :idea:
Already done :) Ok, it's not PooBasic, but Pob-Basic

http://uploads.pob-tech.com/files/docs/ ... tax_en.pdf

Extract from doc :
The Basic syntax of the POB-Basic And all of documentations come from PureBasic:
http://www.purebasic.com/
Please correct my english
http://purebasic.developpez.com/
Justin
Addict
Addict
Posts: 948
Joined: Sat Apr 26, 2003 2:49 pm

Re: Classes in PureBasic

Post by Justin »

+1 before the thread get closed
User avatar
bbanelli
Enthusiast
Enthusiast
Posts: 544
Joined: Tue May 28, 2013 10:51 pm
Location: Europe
Contact:

Re: Classes in PureBasic

Post by bbanelli »

-1 ('cause I don't really like them)

Although this types of threads were quite frequent while I was skimming through forum, there are more pressing matters and bug fixes that can make PureBasic even better than it currently is and it does not include OOP.
"If you lie to the compiler, it will get its revenge."
Henry Spencer
https://www.pci-z.com/
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Classes in PureBasic, ignorance from warped minds.

Post by heartbone »

I won't call the advocates of OOP any vile names, I'll just make the observation that they are victims of an evil plot.
If that crap, excuse me, classes were to be made part of the language then...
1) the language would no longer be BASIC
2) it would take me many times longer, perhaps over a decimal order of magnitude, to get anything accomplished.

As it is PB is a really useful tool, and if the OOP proponents were to be accomodated, then it would become CRAP.

For example, I recently decided to make a game that I did not even know much about until less than a month ago.
I started the interface layout and creating the graphics on Sunday, October 26, 2014.
My goal is for the game to be easy to play using only the arrow keys.
There will be no fancy eye candy, no awesome sound effects, nor music in this bare bones version.
I am aiming for the game's logic to be stripped down to the bare essentials,
with any extra steps unrelated to the deductive logic elements removed from game play.
Today I'm working on ALPHA.19 and I'll have a playable BETA in an estimated fifteen to twenty hours of additional work.
If I were using C++, then I'd still be designing the classes.

Dammit Jim, I'm a programmer, not a computer science professor.
Keep it BASIC.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Classes in PureBasic, ignorance from warped minds.

Post by luis »

heartbone wrote: 1) the language would no longer be BASIC
It's quite distant from classic Basic already, luckily. Pointers in BASIC ? AH!
heartbone wrote: 2) it would take me many times longer, perhaps over a decimal order of magnitude, to get anything accomplished.
You could simply not use classes.

The problem is the code posted in the forum would certainly change a lot, and probably around 50% of PB user would have an hard time to follow it. Also since PB cannot become object oriented at its core, it would be a procedural Basic with Classes, similarly to C++ that is C with classes.
You would found in the forum good OOP code, good procedural code, crappy OOP code, crappy procedural code, horrible mixed code, sensible mixed code, etc. and only 10% of the forum members would be able to navigate it and adapt it when needed as needed.

Now you just have good and crappy procedural code with some mixed nuance introduced by the modules which are still procedural code inside a box with a very low entry barrier.

Anyone can participate and tinker with what posted, and that's what Fred seems to want according to what he said in the past.
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply