OOP Support (it is time)

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Zizaco
User
User
Posts: 26
Joined: Tue May 16, 2006 8:20 pm
Contact:

Re: OOP Support (it is time)

Post by Zizaco »

After doing some testing, I came to a conclusion which is the closest to make a Cross Dependency Between Classes using SimpleOOP. However there are still severe limitations since the PB Interface statment cannot be used to declare the classes as I thought.

Non OO code for demonstration:

Code: Select all

;- Structure Person
Structure Person
	Name.s
	*Where.Room ;<---- Cross Dependency Between Classes
EndStructure

;- Structure Room
Structure Room
	Label.s
	List *Peopple.Person()
EndStructure

;Instantiate a Room ---------------------------------
*objRoom.Room = AllocateMemory(SizeOf(Room))
InitializeStructure(*objRoom, Room)

*objRoom\Label = "Meeting Room"

;Instantiate and insert peopple1 ---------------------
AddElement(*objRoom\Peopple())
*objRoom\Peopple() = AllocateMemory(SizeOf(Room))
InitializeStructure(*objRoom\Peopple(), Room)
*objRoom\Peopple()\Name = "Billy"
*objRoom\Peopple()\Where = *objRoom

;Instantiate and insert peopple2 ---------------------
AddElement(*objRoom\Peopple())
*objRoom\Peopple() = AllocateMemory(SizeOf(Room))
InitializeStructure(*objRoom\Peopple(), Room)
*objRoom\Peopple()\Name = "Bob"
*objRoom\Peopple()\Where = *objRoom

;Print Results ---------------------------------------
Debug "RoomLabel: "+*objRoom\Label
ForEach *objRoom\Peopple()
	Debug "     Person: "+*objRoom\Peopple()\Name+" are in the "+*objRoom\Peopple()\Where\Label
Next

;ownTheRoom ------------------------------------------
;In this case, this is alot redundant, but is for comparison purposes only
*objRoom\Peopple()\Where\Label = *objRoom\Peopple()\Where\Label+" of "+*objRoom\Peopple()\Name ;*objRoom\Name = "Meeting Room of Bob"
The supposed object-oriented version:

Code: Select all

;- Structure Person
Class Person
	Private Name.s
	Private *Where.Room ;<---- Cross Dependency Between Classes
	
	Public Method setName(Name.s)
		This\Name = Name
	EndMethod
	
	Public Method.s getName()
		MethodReturn This\Name
	EndMethod
	
	Public Method setRoom(*obj.Room) ;<---- Cross Dependency Between Classes [Error]
		This\Where = *obj
	EndMethod
	
	Public Method.Room getRoom() ;<---- Cross Dependency Between Classes [Error]
		MethodReturn This\Where
	EndMethod
	
	Public Method ownTheRoom()
		;THIS WILL NEVER WORK UNLESS THERE IS SOME "PROCEDURE DECLARE" LIKE COMMAND FOR CLASSES
		*obj.Room ;<---- Cross Dependency Between Classes [Error]
		*obj = This\Where
		*obj\setLabel(*obj\getLabel()+" of "+This\Name) ;<---- Cross Dependency Between Classes [Error]
	EndMethod
EndClass

;- Structure Room
Class Room
	Private Label.s
	Private List *Peopple.Person()
	
	Public Method setLabel(Label.s)
		This\Label = Label
	EndMethod
	
	Public Method.s getLabel()
		MethodReturn This\Label
	EndMethod
	
	Public Method addPeopple(*obj.Person)
		AddElement(This\Peopple())
		This\Peopple() = *obj
	EndMethod
		
	Public Method getPeopple()
		MethodReturn This\Peopple()
	EndMethod
	
	Public Method Show()
		Debug "RoomLabel: "+This\Label
		ForEach This\Peopple()
			*obj.Room = This\Peopple()\getRoom()
			Debug "     Person: "+This\Peopple()\getName()+" are in the "+*obj\getLabel() ;[Error]
		Next
	EndMethod
EndClass

;Instantiate a Room ---------------------------------
*objRoom.Room = NewObject.Room()
*objRoom\setLabel("Meeting Room")

;Instantiate and insert peopple1 ---------------------
*objPerson.Person
*objPerson = NewObject.Person()
*objPerson\setName("Billy")
*objPerson\setRoom(*objRoom)
*objRoom\addPeopple(*objPerson)

;Instantiate and insert peopple2 ---------------------
*objPerson = NewObject.Person()
*objPerson\setName("Bob")
*objPerson\setRoom(*objRoom)
*objRoom\addPeopple(*objPerson)
 
;Print Results ---------------------------------------
*objRoom\Show()

;ownTheRoom ------------------------------------------
*objPerson\ownTheRoom() ;But, due the line 24, it will never works
This was the closest code I got from the code shown above that was able to compile:

Code: Select all

;- Structure Person
Class Person
	Private Name.s
	Private *Where.Room ;<---- Cross Dependency Between Classes
	
	Public Method setName(Name.s)
		This\Name = Name
	EndMethod
	
	Public Method.s getName()
		MethodReturn This\Name
	EndMethod
	
	Public Method setRoom(*obj)  ;<---- Omitted the Where type/class
		This\Where = *obj
	EndMethod
	
	Public Method getRoom()  ;<---- Omitted the Return  type/class
		MethodReturn This\Where
	EndMethod
	
	Public Method ownTheRoom()
		*obj = This\Where
		;THIS WILL NEVER WORK UNLESS THERE IS SOME "PROCEDURE DECLARE" LIKE COMMAND FOR CLASSES
		;*obj\setLabel(*obj\getLabel()+" of "+This\Name) ;<---- Cross Dependency Between Classes [Error]
	EndMethod
EndClass

;- Structure Room
Class Room
	Private Label.s
	Private List *Peopple.Person()
	
	Public Method setLabel(Label.s)
		This\Label = Label
	EndMethod
	
	Public Method.s getLabel()
		MethodReturn This\Label
	EndMethod
	
	Public Method addPeopple(*obj.Person)
		AddElement(This\Peopple())
		This\Peopple() = *obj
	EndMethod
		
	Public Method getPeopple()
		MethodReturn This\Peopple()
	EndMethod
	
	Public Method Show()
		Debug "RoomLabel: "+This\Label
		ForEach This\Peopple()
			*obj.Room = This\Peopple()\getRoom()
			Debug "     Person: "+This\Peopple()\getName()+" are in the "+*obj\getLabel(*obj) ;<--- Really wierd, since that
																				;Method has no parameters!
		Next
	EndMethod
EndClass

;Instantiate a Room ---------------------------------
*objRoom.Room = NewObject.Room()
*objRoom\setLabel("Meeting Room")

;Instantiate and insert peopple1 ---------------------
*objPerson.Person
*objPerson = NewObject.Person()
*objPerson\setName("Billy")
*objPerson\setRoom(*objRoom)
*objRoom\addPeopple(*objPerson)

;Instantiate and insert peopple2 ---------------------
*objPerson = NewObject.Person()
*objPerson\setName("Bob")
*objPerson\setRoom(*objRoom)
*objRoom\addPeopple(*objPerson)
 
;Print Results ---------------------------------------
*objRoom\Show()

;ownTheRoom ------------------------------------------
;*objPerson\ownTheRoom() ;But, due the line 24, it will never works
As you can see, there is a limitation in cross dependency when you need to call methods between classes. In this case the method ownTheRoom().

If we swap the declaration order of the classes then the Show() method would be the problem, because it wouldn't be able to access the Person method getName().

I guess there will not be an truly Cross Dependency support until unless there is a Declare/Interface (Like de Procedures Declare) command for the classes.

My conclusion is that SimpleOOP is not a usable solution for OO programming in PureBasic. There is another OO precompiler. The PureObject, I wonder if anyone knows if it has this same limitation.
User avatar
TomS
Enthusiast
Enthusiast
Posts: 342
Joined: Sun Mar 18, 2007 2:26 pm
Location: Munich, Germany

Re: OOP Support (it is time)

Post by TomS »

Your problem has nothing to do with oop.
It's a PB restriction.
Simple OOP is not a maigc tool that fixes PB or adds new features.
It's just pre-compiler that makes PB-syntax from pseudo-syntax.

Code: Select all

Structure Room
   Label.s
   List *Peopple.Person()
EndStructure

Structure Person
   Name.s
   *Where.Room ;<---- Cross Dependency Between STRUCTURES
EndStructure
And what's that supposed to mean?

Code: Select all

Debug "     Person: "+This\Peopple()\getName(*obj)+" are in the "+*obj\getLabel() ;<--- Really wierd, since that
                                                            ;Method has no parameters!
Why are you trying to use *obj as a parameter? This results in an error. What's weird about that?
User avatar
Zizaco
User
User
Posts: 26
Joined: Tue May 16, 2006 8:20 pm
Contact:

Re: OOP Support (it is time)

Post by Zizaco »

Ok, I did not know that it was a limitation of PB. Thats really bad that PureBasic will never have an usable OOP support.

Can't be that hard for the PB authors to do (correctly) what those precompilers are trying to do for years and keep all the old(thats the word) structural programming support. I know alot(most) of you hate OO and don't think is necessary, but there is alot of people that do. So why not to implement that new feature since there is nothing to lose by doing it!?

The community(gnozal in this case) made an IDE that supports Projects, since alot of users was demanding it. Later the PB team added (after some time) that same feature to the PB editor(which i think most of you liked). That makes sense to me.

The same happened with some Tools that later has become PB features. I guess the Tools menu of PB editor is full of those.

When someone makes a new tópic about OOP people say things like:
Shield wrote:...and again...
DaylightDreamer, please do a quick search to find at least one of those two thousand threads where this has been discussed...
"two thousand threads" is an pretty evidence to any inteligent creature that the customers are asking for OOP support.

The community demands, obviously, what they would like in PB to make it even better.
And as a commercial product, they should worry about the customer's opinion. Whats the logic of ignoring it?

The only ways to make customers to stop asking for OOP support is attending them or not having them.
shadow wrote:Small projects I do develop in PB, larger ones in Java, C# or C++.
I guess that's it. In other words, BP is digging his grave.

TomS, i'm supposed to mean that:

Code: Select all

Debug "     Person: "+This\Peopple()\getName()+" are in the "+*obj\getLabel(*obj) ;<--- Really wierd, since that Method has no parameters!
If you don't use the *obj parameter in the last getLabel() call it gives me an error saying "not enought parameters". Not sure why. But that doesn't matter much now.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: OOP Support (it is time)

Post by PMV »

Zizaco wrote:Can't be that hard for the PB authors to do (correctly) what those precompilers are trying to do for years and keep all the old(thats the word) structural programming support. I know alot(most) of you hate OO and don't think is necessary, but there is alot of people that do. So why not to implement that new feature since there is nothing to lose by doing it!?
People who can't program without OOP can't really call himself a
programmer. And yes, when you make it right, you can make any
program without OOP ... but of course, some things are easier.
Other's aren't. (anyone who thinks he was attacked by this one
will make a come out if he will write that :mrgreen: )

And no, i doesn't hate OOP.
I doesn't say it is not a nice to have.
And i doesn't even say PB should never implement it for optional use.
If PB can OOP, i will use it, too. Heavily used :shock:

... but it is really annoying to see people screaming "if PB doesn't
have OOP it will not have a future". And yes, if you say it like you
have ... that's your statement. :twisted:

At first it is a statement of the developer, no OOP, why repeating
it over and over again? :?

And you see what PB can. You see what a single (a few people now)
can accomplish. But any human has his limit's. This will be no
improvement in the functionality itself of PB. So the time it will need
to implement full OOP (and it is a really huge amount) is time where
bugs can't be fixed and of course ... there will be new bugs. If you
want full OOP ... the only choice you have is to use another language.
:)

And to repeat it for the fan-boys: It is normal to use many languages,
that language, that fits best for a problem to help solving it, not to
love and use only one. And if OOP is needed, then PB isn't it :wink:


Happy Coding ... 8)

MFG PMV
X
Enthusiast
Enthusiast
Posts: 311
Joined: Tue Apr 04, 2006 6:27 am

Re: OOP Support (it is time)

Post by X »

Well, could always go the C / C++ route ... PB for structural programming while PB++ for OOP only. Charge a bit more for PB++. I'll definitely bite on that.
User avatar
Zizaco
User
User
Posts: 26
Joined: Tue May 16, 2006 8:20 pm
Contact:

Re: OOP Support (it is time)

Post by Zizaco »

PMV wrote:People who can't program without OOP can't really call himself a
programmer. And yes, when you make it right, you can make any
program without OOP ... but of course, some things are easier.
Other's aren't.
I totally agree with you. In fact you can make anything without OOP or even using only assembly. :wink:
But the point is that some things make it easier. Other don't. (things = tools, languages, paradigms)
PMV wrote: And no, i doesn't hate OOP.
I doesn't say it is not a nice to have.
And i doesn't even say PB should never implement it for optional use.
If PB can OOP, i will use it, too. Heavily used :shock:
So, in your opinion, it should be done.
PMV wrote: ... but it is really annoying to see people screaming "if PB doesn't
have OOP it will not have a future". And yes, if you say it like you
have ... that's your statement. :twisted:
Ok. This is a personal opinion of mine (and of many others). And from a commercial perspective thats what i think.
PMV wrote:If you want full OOP ... the only choice you have is to use another language.
Yeah, unless the PB dev team listen to the users.

In my opinion PB is the best structural language. Unless we're speaking of very specific scenarios, PB should solves everything easly than any other tool. (And that's why I'm here.)
Why not to take this reality to the OOP world?

I'm not asking to come to this site tomorrow and see that OOP support is implemented. I'm just want that the devs assume that it is a good idea and that, sooner or later, should be done.
User avatar
Blood
Enthusiast
Enthusiast
Posts: 161
Joined: Tue Dec 08, 2009 8:34 pm
Location: United Kingdom

Re: OOP Support (it is time)

Post by Blood »

The real issue at hand is that the PB team really don't care about adding OOP. In fact to do it properly the entire language would need redesigning from the ground up. If you look at previous threads the answers are always the same. This is not Fred's full time job and he sees it only as a hobby.

I personally only use an OOP language for serious stuff (as do most professional programmers) so PB is really for toying with but that rather sums up the community here. Nothing major is ever created with PB* and most of the community are beginners. It's pointless wasting time arguing the point. PB is BASIC, and BASIC is a beginners language. The problem is that most BASIC programmers never grow past that initial language. I think Dijkstra said it best:
Edsger W. Dijkstra wrote:I think of the company advertising "Thought Processors" or the college pretending that learning BASIC suffices or at least helps, whereas the teaching of BASIC should be rated as a criminal offence: it mutilates the mind beyond recovery.
Die hard BASIC programmers always tend to be old men who took programming up as a retirement hobby or someone too inexperienced to understand there are always better tools than BASIC available for any given task. I mean, i saw a thread here not so long back about using PB for web programming!!! purlease! Commercial, procedural languages are toys! nothing more.

*There are a few people who create larger programs with PB but they are in a very small minority. And no company bigger than one man would ever allow a code base in PB to exist.
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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: OOP Support (it is time)

Post by freak »

Yawn! This whole "If only PB had X, it would be a 'professional' language" argument is so old. And its plain false too.

This is getting nowhere.
quidquid Latine dictum sit altum videtur
Locked