PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Object-Oriented programming in PPL?

Anyone interested in seeing object-oriented programming into PPL?

I should be able to implement it for beta 0.9 and it will be a bit like what python offers in term of OO.

Something like:

Code:
class <classname> <inheritfromclassname> {
var$ = value;

proc test (var2$)
var$ = var2$
end;
}

newclass$ = classname$;
newclass.var$ = 30;
newclass.test(100);


This is just a prototype but it will be very simple and effective Object-Oriented programming.

Any thoughts or ideas?
Kornalius,

If you have time for this further development I think it is the next logical addition. It would make PPL "complete"
I want to first give beta 0.8 some time to make sure it's alright, release a few patches and then work actively on 0.9 with Object-Oriented.

I need to finish the Visual Form Builder for PPL and the ads, then 0.8 beta will be released.
I like the idea of OO extensions.

I would suggest keeping it simple:
- allow function overloading - NO operator overloading.
- allow public and private - NO protected or friend.
- allow single inheritance - NO multiple inheritance.

The flashy bits can always be added later.

Could you tell us a little bit more about what you have in mind?

Brad
So far, very simple:

- Multiple inheritance, not harder to implement that single.

- Proc/Func overload with overwrite statement.

- No operators support.

- All variables published. No private or public.

If my design (in mind right now) works, you will be able to save and load compiled objects from disk as a .ppc file. Really cool idea I think. Let's say I make an object, then I can distribute this object as a .ppc (compressed and encrypted) and the user can load it with a simple command, like:

Code:
LoadObject ("\\My Documents\\Obj.ppc", "MyObject");
MyObject.Filename$ = "\\My Documents\\bitmap.bmp";
MyObject.LoadBitmap;
Clear(MyObject$);


Technically an object will be an application for PPL. I will have it's own environment and won't interfere with the main program. I still have to figure out how objects will be copied to another object.

But I am getting there, simple design, complex implementation, easy and clean result I think. And a very expandable object-oriented system. You can modify the code of a class while the program is running. The code will be stored as a string into the class definition variable and then when an object is inherited from the class, the code will be compile dynamically and executed as needed.
Kornalius,

Nice and clean and simple. And you have some very cool ideas! I like it.

Brad
Here are a few updates on the design of the object-oriented language implementation in PPL.

I have most of the internal design on paper. Shouldn't take too long to implement (a week or so) and is quite cool.

To define a class, you use the #class and #endclass compiler directives. Inside you can define variables and proc/func's.

Code:
#class object
ClassName = _ClassName$
#endclass

#class string object
value$ = "";

proc create
// put create code here
end;

proc dispose
// put dispose code here
end;

proc set (string$)
value$ = string$;
end;

func get
return (value$);
end;

proc deletestr (index$, count$)
value$ = delete(value$, index$, count$);
end;

proc trimstr
value$ = trim(value$);
end;
#endclass

proc main
s$ = string;
s$ = "THIS IS A STRING";
s.deletstr(0, 5);
ShowMessage(s$);
dispose(s$);
end;


I have not finalized the get/set functions yet but they will certainly be implemented.

What do you guys think?
PointOfLight · 18 Sep 2004, 04:42 UTC
This is going to be sweet! I think I'll turn my INI library into a class when it's ready 8)
It will be the perfect library to convert to a class, no doubt. 8) I will start implementation early next week and should have a OO-build by mid-week probably.

I will also be running a profiler and memory analyzer tool on PPL during the next week to find problems, memory leaks and to optimize PPL a bit more. I want to make sure PPL is very stable for 0.8.

This v0.8 is going to be a huge release compared to previous builds. Object-Oriented, Visual Form Builder for PPC, Intel Version, optimized and more stable, more stable debugger, matrices and so much more...

I think (and hope) PPL will prove to be more mature and starting to be ready for an official release. I want to give it the time it needs though. I will still give myself a few more beta releases until then but I think it's shaping up pretty good. I am starting to see the light at the end of tunnel.
PointOfLight · 18 Sep 2004, 04:56 UTC
The amount of things you have added to PPL in the last few months is mind boggling. I hate to keep comparing, but I think of a product that has been around as long as NSBasic has, and I don't think it has as many features as PPL does. I really hope you find an audience with this, because so far it's the most enjoyable development product I've used for the PPC.
Kornalius,

This is looking fantastic ... I don't know where you find the time.

Even "simple" OO capability will be a tremendous benefit. My first thought would be for an ADO-based object to mimic some of the more classical language implementations of Database,Table and Query objects. Its all looking very possible.
How do you guys like the idea of being able to load and create classes dynamically while the program is running? The class doesn't have to be hard-coded at design time. I would like to know the interest level of this feature before I start implementing OO in PPL.
PointOfLight · 27 Sep 2004, 02:18 UTC
That would certainly be interesting, but what real benefit would it serve? I'm not trying to be critical, this is just an honest question. The only benefit I would see off hand would be to facilitate a scripting language, but since PPL already is one, that wouldn't make much sense. What are you seeing as the potential uses of this, kornalius?
I started work on the Object-Oriented syntax today and so far, I got it working without inheritance fully working. It's a good start though. I should have it fully operational by the end of the week.

Concerning dynamic class definition, I will not do it. I came to the conclusion that it is not following the principals of an oo language.

At the same I simplified a few things within the core of the system and was able to speedup the execution speed a bit. I got rid of a few unused or barely used system functions.

I also modified the ^ operator to accept only one simple expression. If one need to do pointer's arithmetic the ptr() will do the job.
Kornalius,

I have been busy with another project (several actually) but I saw this and wanted to give my opinion.

From the time I first heard this plan to load objects dynamically, I immediately thought of object serialization; or more simply put... the ability to write an object (save the state of the object) out to a file. For example, if you had a class that was calculating Pi to a million decimal places, then the object could be saved to a file and loaded & run again from the point where you saved it.

I hope something like this could be included in a future version.

Brad
Loading and saving objects might be implemented in a near future. I just want the Object-Oriented system to work properly first.

I've got a complete object-oriented system implemented with unlimited inheritance and published and local variables.

Here is a simple stupid code that i've used to test:

Code:
// Class test

proc test2(y$)
ShowMessage("test2 y$=" + y$);
end;

#class object
global(z$);
z$ = 1000;

nproc test3
ForEach(args$)
ShowMessage(lpos(args$) + " = " + args$);
end;
end;

#endclass

#class object myclass
global(i$);
x$ = 100;
i$ = 10;
ShowMessage("INIT: " + i$ + "," + x$);

proc test2(y$)
ShowMessage("y$ + i$ + z$ =" +(y$ + i$ + z$));
test3(10, 20, 30);
end;

proc test(a$)
ShowMessage("a$ + i$ = " +(a$ + i$));
test2(100);
end;
#endclass

proc main
#object o$ object;
ShowMessage(o.z$);
ShowMessage(o.i$);

#object c$ myclass;
ShowMessage("c.i$ = " + c.i$);
ShowMessage("c.z$ = " + c.z$);
ShowMessage("c.x$ = " + c.x$);
i$ = 50;
ShowMessage("i$ = " + i$);
c.test(20);
c.test2(200);
test2(300);
end;


It tests various aspect of the object-oriented syntax. Note that NPROC and NFUNC are valid within a class definition.

There are a few things to keep in mind when creating an object. First, this initialization is done when declaring variables and the published variables needs to be global to the object's class.

Second, objects are created with the #object compiler directive. The compiler needs to know at compile time what class the new object variable is in order to call the right procedures/functions. Therefore, if you need to copy an object to another, you need to tell the compiler with a #object directive that the variable will be an object variable.

ex:
Code:
#object o1$ myobject
o1.var$ = 10;
#object o2$ myobject
o2$ = o1$;
Here is an update:

New functions Published() and Public() instead of Global() and Local() are now used.

Code:
// Class test

proc test2(y$)
ShowMessage("test2 y$=" + y$);
end;

#class object
published(z$);
z$ = 1000;

nproc test3
ForEach(args$)
ShowMessage(lpos(args$) + " = " + args$);
end;
end;

#endclass

#class object myclass
published(i$);
public(x$);
x$ = 100;
i$ = 10;
ShowMessage("INIT: " + i$ + "," + x$);

proc test2(y$)
ShowMessage("y$ + i$ + z$ =" +(y$ + i$ + z$));
test3(10, 20, 30);
end;

proc test(a$)
ShowMessage("x$ + a$ + i$ = " +(x$ + a$ + i$));
test2(100);
end;
#endclass

proc main
#object o$ object;
ShowMessage(o.z$);
ShowMessage(o.i$);

#object c$ myclass;
ShowMessage("c.i$ = " + c.i$);
ShowMessage("c.z$ = " + c.z$);
ShowMessage("c.x$ = " + c.x$);
i$ = 50;
ShowMessage("i$ = " + i$);
c.test(20);
c.test2(200);
test2(300);

c.i$ = 3000;
ShowMessage(c.i$);
ShowMessage(c.x$);
#object c2$ myclass;
c2$ = c$;
c2.test(50);

end;
How important do you guys think operators in classes are? I personaly don't think it's harder to do:

Code:
object.value$ = 10;
if (object.value$ > 10)
end;


then:

Code:
object$ = 10;
if (object$ > 10)
end;


The more I think about it, the less I feel like implementing it :lol:. It will probably just make PPL a little slower in the end without gaining much.
Kornalius,

When you say:
How important do you guys think operators in classes are?
Do you mean operator overloading or are you talking about using operators to compare fields in a class?

I can't really tell what you mean from your example. Could you please give a more complete example?

When you say that it will slow down PPL, do you mean everyone or just the people using OO in their programs?

Personally, if you are talking about compiler speed, or if you are referring to the speed issue slowing down just the people using OO then I would stick closely to the OO paradigm. If it would slowdown every program, then that would effect developers trying to program games and that may be bad and the answer is more fuzzy.

Brad
I am talking about operators overloading. Sorry.

Something like:

Code:
#class myclass
Published(value$);
value$ = 10;

func _operator_gt (compare$)
return (value$ > compare$);
end;
#endclass

proc main
#object object$ myclass
if (object$ > 5)
ShowMessage("Greater!");
end;
end;


I hope I am understanding operators overloading properly because I am not a big C++ expert. The Object-Pascal used in Delphi doesn't have operators overloading as far as I remember, that is why I am not very familiar with them.

One thing I can do is to implement a _get and _set default functions in the class definition to get or set the default object property value.

Code:
#class myclass
Published(value$);
value$ = 10;

func _operator_get
return (value$);
end;
#endclass

proc main
#object object$ myclass
if (object$ > 5)
ShowMessage("Greater!");
end;
end;


It would do the same as above and I wouldn't have to implement extra calls within the standard operators, therefore not slowing down the other programs.
Kornalius,

I'd say don't implement operator overloading at all. I'll give a short explanation.

Consider for a moment 2 objects A and B. To copy the value of B to A we would write A = B. (Stay with me, it gets better.) What does this statement do for the object? No one knows. The creator of the object would presumably overload the = operator and create a method that would do the copy. (In C++ the compiler will create a default action for the operator, but I will almost guarantee you that is will be wrong.)

Now imagine that this object contained an integer and a string. The overloaded = operator code might look like this:

Code:
#class myObj$
public tmpStr$
public tmpInt$
// overloaded =
proc = (srcObj$, destObj$)
destObj.tmpInt$ = srcObj.tmpInt$;
destObj.tmpStr$ = srcObj.tmpStr$;
end;
#endclass


If this were C++, there would be a problem. In the string assignment, the address of the source string would be copied to the address for the destination string. Following the assignment, if the source string were deleted, then the destination string would have a reference to freed memory. So to make it work, you must allocate a new string object and set it to the chars from the source string and the destination object would then point to the new memory. So for a complex class the = operator would need to have a method with many steps.

Now extrapolate for the > operator. How to compare A with B? Compare lexically on the string and if equal look at the int? The other way around? The designer of the class decides because for a complex class, there may be many steps in determining which is greater.

So, with the background out of the way, why "no" to operator overloading? First, I think it would get very complex to do it "right". Second, any lesser solution with a single getter and setter would just get in the way. Third, there is no implementation of private class variables.

Private vars (in my opinion) make operator overloading necessary to a certain extent. It would be impossible for some procedure or method outside the class to copy the exact state of the object if there was a private var with no getter and setter.

OK, maybe that wasn't so brief, but it sounded like you were a little fuzzy on how they actually worked in C++.

Come on gang. Speak up. Lets here some other opinions.

Brad
PointOfLight · 30 Sep 2004, 02:56 UTC
In my opinion, I'm sure there are good reasons to use them (and maybe I just didn't because I wasn't a good C++ programmer), but I never bothered with them. I'd say leave them out unless you have a really good argument for them.
<De-lurk>
Why not follow something like C# / (ECMAScript)'s operator system - allow overloading for the comparison, binary and unary operators, but disallow overloading of the assignment operator (and other similar cases if your language supports them).

Have a look at <a href="http://www.csharphelp.com/archives/archive135.html"> this</a>
</De-lurk>
berwan03 · 30 Sep 2004, 11:28 UTC — operator overloading
Hello Kornalius,

I also think that if implementing operator overloading would be a bit complex to implement and would also slow down not OO programs, why do it ?Assuming that you can simply create methods to perform these operations. :o
But to be honest I do not understand what you mean by:

#class myclass
Published(value$);
value$ = 10;

func _operator_get
return (value$);
end;
#endclass

proc main
#object object$ myclass
if (object$ > 5)
ShowMessage("Greater!");
end;
end;

a setter and a getter is always lined to a object property, in your example when you say
if (object$ > 5)
it looks like you are testing object itself, but suppose I derive myclass like:

#class myclass newclass
Published(newvalue$);
newvalue$ = 10;

#endclass

what should I put in _operator_get ?
return value$ and newvalue$ ? but what would be the test ?
getter and setter have reason if you can private attributes.
I am sick this week, maybe I missed something. :oops:
Hi guys,

Alright after a few moments of thinking... (Yes it happens :wink:) I decided not to complicate things too much within the oo of PPL. I will leave it simple and effective. Like you guys, I think I shouldn't implement a setter and getter methods. The other simple operators (<, >, <>, ==, >=, <=) it might slow down PPL a little touch if I implement this. However if you guys think it is necessary I will implement them.

Here is where I stand with the oo as of now. I changed the published and public to Public and Private. I also now allow for forward methods declaration. It makes a clearer class definition too. I have implemented the create and destroy methods that are called on object creation and destruction.

Code:
#class myclass
Public(i$);
Private(x$);
i$ = 10;
x$ = 50;

forward proc create
forward proc destroy
private forward proc test
forward proc test2

#endclass

proc myclass.create
ShowMessage("Creating " + Self$ + "!");
end;

proc myclass.destroy
ShowMessage("Destroying " + Self$ + "!");
end;

proc myclass.test
ShowMessage(self$ + "=" + &self$);
ShowMessage(self.i$ + "," + i$);
ShowMessage(X$);
end;

proc myclass.test2
test;
end;


PPL OO, supports private and public methods and variables.

Thanks for your ideas and time.
PointOfLight · 30 Sep 2004, 15:41 UTC
That looks sweet. So how long before we get our hands on this puppy?
Tomorrow sounds good? :D

I will put the finishing touch on a few little things and you guys will get the latest build. There are lot of things fixed in this build.

Btw, how is the help file coming along, I will need to write a section on Object-Oriented real soon. :wink:
PointOfLight · 30 Sep 2004, 16:27 UTC
Tomorrow sounds great! I'm not quite ready to send the help file back yet, but if you want, go ahead and write the pages on OO, send me the new help file, and I will merge them into the help file I am working with (just don't add them to the table of contents or anything quite yet). In fact, you can do that with any new items you might need to add.
Sounds good to me. I will send you the new help file then tomorrow.
Here is the latest OODEMO.PPL file. This version shows constructors and destructors as well as the ability to pass arguments to the create constructor.

Code:
// Object-Oriented programming, written by Alain Deschenes (c) ArianeSoft 2004

/* Let's create the class employe. This class will hold all pertinent
information about our employes. The only function we have is used to
automatically calculate the salary an employe should be getting based
on his age, experience and base salary. */

#class employe

/* This is the initialization section. It called right when the object
is created in memory. The constructor (Create) is called right after.
You can put any valid code in here too. */

Public(Name$, Age$, Experience$, Salary$);
Private(BaseSalary$);
BaseSalary$ = 20000;
Name$ = ObjectName$; /* ObjectName$ is an internal variable that
contains the name of the variable that holds the class
without the $ or % sign. */

/* Define the class constructor */
public forward nproc Create;
/* Define the class destructor */
public forward proc Destroy;

/* Simple forward public function declaration */
public forward func CalcSalary;
#endclass

/* Any number of parameters can be passed to the Create constructor.
The constructor is called right after the initialization section. */
nproc employe.Create
if (Count(Args$) >= 1)
Name$ = Args$[0];
end;
if (Count(Args$) >= 2)
Age$ = Args$[1];
end;
if (Count(Args$) >= 3)
Experience$ = Args$[2];
end;
end;

/* The destructor procedure is called when the object is freed from memory. */
proc employe.Destroy
ShowMessage(Name$ + " object is destroyed!");
end;

/* This function will return the employe's yearly salary based on his
age and experience. */

func employe.CalcSalary
if ((Age$ >= 18) and (Age$ < 30))
i$ = 5000;
else if ((Age$ >= 30) and (Age$ < 40))
i$ = 15000;
else if ((Age$ >= 40) and (Age$ < 65))
i$ = 25000;
end;
i$ += BaseSalary$;
Salary$ = i$ + (i$ * ((Age$ * (Experience$ / 40)) / 100));
return (Salary$);
end;

#class employe boss
Public(Share$);

public nproc Create
#inherited Create(Args$); // Call employe.create constructor.
Share$ = 10; /* You need to have 10% of the company's shares in order to
be a boss. */
end;

/* Since this the boss's class, he must own a part of the company. */

func CalcShare
return (Salary$ + ((Share$ / 100) * Profit$ + Profit$));
end;

/* Calculates the boss's salary from the employe's CalcSalary function but
add a 50,000$ bonus to this. */

public func CalcSalary
#inherited CalcSalary;
Self.Salary$ += 50000; /* Remark the Self$ variable here. It is a variable
pointing to your original object variable. */
CalcShare;
return (Salary$);
end;
#endclass

proc main

/* This is the profit we did in the year. Not bad hein? */

Global(Profit$);
Profit$ = 1500000;

/* Joe is our company's greatest asset. He has been here for a while and
he is old enough to be serious about his work. */

#object joe$ employe("Joe McCornik", 35, 15);
ShowMessage(joe.Name$, " = ", fstr(joe.CalcSalary) + "$");

/* Bob is a young employe without much experiences, therefore his
salary is a little low */

#object bob$ employe("Bob Brown");
bob.Age$ = 22;
bob.Experience$ = 4;
ShowMessage(bob.Name$, " = ", fstr(bob.CalcSalary) + "$");

/* Clark is a boss in our company. His salary is calculated the same
way as other employes, except he gets a 50,000$ bonus. That is not all,
Clark also owns 25% of the company's shares. His share of the annual
profit is also added to his salary. */

#object clark$ boss("Clark Robson");
clark.Age$ = 50;
clark.Experience$ = 25;
clark.Share$ = 25;
ShowMessage(clark.Name$, " = ", fstr(clark.CalcSalary) + "$");

/* NB: All objects are automatically freed when PPL exists or when
the scope where the variable belongs is exited. */
end;
A few updates on the OO system:

Changes:
- #object <classname> <variable$> [(...)] instead of #object <variable$> <classname> [(...)]
- Remove #objectclass directive.
- Replaced by <classname>(variable$,...);

Example:
Code:
#class myclass
private(p$);

public nproc Create
p$ = Args$;
end;

public proc Call
ShowMessage(p$);
end;
#endclass

proc test(obj$)
MyClass(obj$);
Obj.Call;
end;

proc main
#object myclass o$(100);
test(o$);
end;