Classes in PureBasic

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
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
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Classes in PureBasic, ignorance from warped minds.

Post by Tenaja »

heartbone wrote:As it is PB is a really useful tool, and if the OOP proponents were to be accomodated, then it would become CRAP.
With modules, we are nearly there, syntactically. And how about that...code that does not use modules has not changed because of the feature.

Heartbone, you are making the wild assumption that adding features alters existing syntax. The module is the perfect example to disprove that assumption.

Just because you hate a feature does not mean you should be against it. I am not likely to use oop myself, but I would like to have the feature because I think it will open PB up to a wider audience.
User avatar
heartbone
Addict
Addict
Posts: 1058
Joined: Fri Apr 12, 2013 1:55 pm
Location: just outside of Ferguson

Re: Classes in PureBasic

Post by heartbone »

Tenaja wrote:With modules, we are nearly there, syntactically. And how about that...code that does not use modules has not changed because of the feature.
And 1+1 still equals 2.
Heartbone, you are making the wild assumption that adding features alters existing syntax. The module is the perfect example to disprove that assumption.
Wrong and wrong.
Just because you hate a feature does not mean you should be against it.
Wrong.
I am not likely to use oop myself,
How else can you use something?
but I would like to have the feature because I think it will open PB up to a wider audience.
IMO, from a programming perspective that's an illogical justification for such a major change of methodology.
Keep it BASIC.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: Classes in PureBasic

Post by Samuel »

Here's my two cents on the subject.

I'm relatively new to C++, but I've been learning a little by little over the last year. So, I'm not as knowledgeable as others, but I'd like to think I have a little understanding of classes.

From what I've learned so far I'll say that any competent programer can learn how to use a class system with little effort.
It isn't rocket science after all. Just by using them a little bit one will learn how easy they are to use.

Now will classes ever be added to Purebasic. I doubt it, but I won't be against it if the team decides to implement them into Purebasic.

Adding new features will only help Purebasic in the long run. It's just if the features are worth the effort to implement.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Classes in PureBasic

Post by User_Russian »

All who oppose classes probably nothing not know about them! Nobody is forcing you to use OOP, but its implementation should be in PB!
For some task types, they are ideal, and their absence is very difficult to develop and modernize code. Using interfaces and DataSection (To store addresses procedures) greatly complicates the code reduces its readability, and greatly complicates the inheritance. In addition in interface may not be variable, only the function.

Why Fred against OOP? He believes that this language is only for beginners? Then PB should be free and be taught in schools.
In this case, on the main page of the site you need this text.
PureBasic has been created for the beginner and expert alike.
Replace this
PureBasic has been created for the beginner only.
And this text
Experienced coders will have no problem
Replace this.
Experienced programmers would have trouble in program design.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Classes in PureBasic

Post by Danilo »

@User_Russian:
Are you unable to learn any of the plenty OOP languages out there? Why can't you just use D, C#, or Ruby? It's easy.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Classes in PureBasic

Post by User_Russian »

Danilo wrote:Why can't you just use D, C#, or Ruby?
Dependency on .NET or interpreter.
I need a system language and PB in most of the suitable. But there are some drawbacks, including a lack of OOP.
If switch to another language, then at C++, but then on the main page, lie, claiming that PB is suitable for experienced programmers.
If need to switch to another programming language, the PB, only for beginners!
Post Reply