Question about free pascal ...

Well does anybody know how can I create a class or object in freepascal ?

Call up your local high school and see if they are interested in teaching Free Pascal classes?

Unfortunately they are not … :frowning: .

My Pascal is a little bit rusty these days, but I think it’s somelike that


type 
  PObject = ^TObject
  TObject = class
      i:integer;
      s:string;
      next:PObject;
  end;

^ means Pointer in Pascal.
PObject is pointer to TObject.
The order is given. You can’t enter anything between the PObject and TObject line, this is the only exception from declaration rule.

property “next” is pointer to another TObject, so you can use it as a linked array of objects.

You should probably google for some tutorials on the subject.

Edit:
Here is FreePascal reference on the subject
http://www.freepascal.org/docs-html/ref/refch5.html#x53-580005

Basic Pascal
http://www.functionx.com/objectpascal/
http://www.taoyue.com/tutorials/pascal/

Object Pascal
http://www.sunncity.com/Tutorial/Introduction_To_Oop.html
http://www.sunncity.com/Tutorial/Writing_Class_Library.html

Delphi 5 Object Pascal Language Reference
http://info.borland.com/techpubs/delphi/v5/updates/std.html (grab the d5std.zip at the top of the page for all documentation, or just d5oplr.zip for the op lang ref)

Resources
http://codecentral.borland.com/ (requires an account, but worth it)
http://pascal-central.com/

and if you can read these
http://pascal-central.com/OOE-stds.html
http://bdn.borland.com/soapbox/techvoyage/article/1,1795,10280,00.html

There’s not a whole lot of online information that directly relates to teaching Object Pascal, at least that I’ve found… you’d want to get a book.

Expanding on what ondrew wrote, here is a more complete class reference:


program Fruity;
type
  TFruit = class
  // This is an abstract ancestor class. This means you cannot create a TFruit
  // object. Rather you must define and create descendent classes (such
  // as the TApple and TTomato classes defined below).
  private
    FNumberOfWorms: integer;
  protected
    function  GetNumberOfWorms: integer;
    procedure SetNumberOfWorms( i: integer );

    function Name: string; virtual; abstract; // <-- (implement me)
  public
    property numberOfWorms: integer
      read GetNumberOfWorms
      write SetNumberOfWorms;
  end;

  TApple = class(TFruit)
  public function Name: string; override; // <-- (the implementation)
  end;

  TTomato = class(TFruit)
  public function Name: string; override;
  end;

function TFruit.GetNumberOfWorms: integer;
  begin
  result := FNumberOfWorms
  end;

procedure TFruit.SetNumberOfWorms( i: integer );
  begin
  if i < 12
    then FNumberOfWorms := i
    else FNumberOfWorms := 12
  end;

function TApple.Name: string;
  begin
  result := 'Apple'
  end;

function TTomato.Name: string;
  begin
  result := 'Tomato'
  end;


procedure DescribeFruit( fruit: TFruit );
  begin
  writeln( 'You are holding an ', fruit.name, '.' );
  writeln( 'It has ', fruit.numberOfWorms, ' worms in it.' );
  writeln
  end;

var
  apple:  TApple;
  tomato: TTomato;

begin
try apple := TApple.create;
try tomato := TTomato.create;

apple.numberOfWorms := 50;
tomato.numberOfWorms := 2;

DescribeFruit( apple );
DescribeFruit( tomato )

finally tomato.free end
finally apple.free  end
end.

If you use the object keyword instead of class, you’ll loose the automatic constructors and destructors, etc. You’ll have to use new and delete to instanciate and destroy them.

Hmm, Hope this helps.

What is the differnce between a class and an object in freepascal ?

The difference is that a class is an object with built in control methods:
Create, Destroy, and Free.

I was tired when I wrote the last post, so I shouldn’t have said anything about using the object keyword --don’t use it: stick with class.

Also, you really should download and read the Object Pascal Language Reference from Borland. It explains everything and gives ample examples.

Hope this helps.

The more I hear about Pascal (which I’ll admit 99% of which (plus or minus 1%) has come from Duoas), the more I think it’s a language that should be used more often in projects.

I installed the Free Pascal Compiler and downloaded Lazarus. I then started to compile it and was quite glad to see how fast the code compiled:

126672 Lines compiled, 77.4 sec
43878 Lines compiled, 22.3 sec
55126 Lines compiled, 24.3 sec
61276 Lines compiled, 32.4 sec
27851 Lines compiled, 22.8 sec

314803 lines in about 3 minutes

If I recall, Blender was taking about 20-30 mins to compile on my machine. Now Blender 2.33 had around 328,557 lines, so that seems to tie in with average estimates that Pascal compiles around 10 times quicker than C/C++.

:frowning: I want Blender written in Object Pascal. The thing is, you could scrap all 3 languages. Because the compile times are so low and the code is just as readable, you could replace Python with Pascal and then scripts would run as fast your main code. You wouldn’t need C++ because object functionality is built in. The only issue with that would be whether you can dynamically link code - i.e. compile some addon in Pascal and then run it from an application without closing the app and re-linking. If not, you’d still need Python but once Python scripts were made, I bet it would be easier to put them into the app.

I know, it’s too much work and developers would need to learn a whole other language but I sooo want faster compile times. I guess once you get a stable build of an app in C/C++ or whatever then patching it is probably quite fast as long as you don’t change an important dependecy. But every time I tried to compile one of the CVS Blenders, it would reach an error and then after I fixed it, it would start from the beginning again. I wasted a whole afternoon once just waiting for compiling. With Pascal, I probably would still have wasted my afternoon but 9/10ths of it would have been spent looking at pornography instead of watching my IDE and CPU choking to death.

How feasible would a code translation of Blender be though? Not as a replacement persay but as an experiment. Also, what about the stability and performance of the code?

I didn’t make the link that Delphi was sort of the new name for Object Pascal - am I right? I’m sure I was told that before but it didn’t sink in. I’ve seen plenty of jobs around that require Delphi and I wondered what it was. It seems to be quite popular in mainstream business. There was a poll I found of over 4000 people and they said they used Delphi most, then it was Visual Basic and then it was C++. I guess it depends on what businesses you look at as C++ is more popular in the games and 3D industry in general.

Oh, one problem with Lazarus is that I’m getting a link error:

Linking …/lazarus
/usr/bin/ld: can’t locate file for: -lglib-1.2.0
lazarus.pp(113,1) Error: Error while linking
make[2]: *** [lazarus] Error 1
make[1]: *** [ide] Error 2
make: *** [ide] Error 2

Anyway, I think I’m going to learn Delphi/Pascal instead of C++. I’ve always tried to learn C++ but I just don’t get the parts that go beyond C. Pascal seems a bit more comprehendible. I’ll reserve final judgment until I look at it more closely but fast compile times + readable syntax + fast performance = looking good so far.

Wow, I’m famous! :o

Unfortunately, Pascal still lags behind C/C++ in terms of availability. The GNU-Pascal and FreePascal projects, and Lazarus GUI interfacing, are exciting and welcome, but still relatively young (i.e. buggy and limited). There has traditionally been a strong prejudice against Pascal, in large part because of Kernigan’s [in]famous paper, but it has been a mature and robust language for a decade now, and the FreePascal project has improved upon it even more!

If you know what lexxing and parsing means, you’d be surprised to know that [Object] Pascal and C[++] have exactly the same syntax trees (or almost identical anyway, allowing for variations in language).

Compile time speeds are something that always surprises people about Pascal. I don’t think there’s anything out there (including assemblers) that compare with it… Pascal is a one-pass compiler --a wonderful benefit of the language (C allows you to do things [unnecessary things] that obviate this power).

Pascal is also highly optimized and performs as well as a well-coded C/C++ application.

You ask about linking with foreign objects. It is not only possible in Pascal, it’s easier to do than in C/C++.

As for recoding Blender… that’s unlikely to happen unless you’ve personally got a lot of time on your hands… though I agree that it would be really neat! Have you seen this pascal scripting solution? (If you’ve used Inno Setup Compiler the answer is probably ‘yes’.)

Oh yes, your last question. Delphi is a RAD (Rapid Application Development) environment for writing Win32 applications sporting Borland’s Object Pascal as its language of choice. The closest Unix equivalent I can think of is older versions of KDevelop with Trolltech’s Qt 3: it has an integrated editor, compiler, forms designer, and component palette (standard window controls, OLE/COM containers, etc.).

For your error, first check that you have the latest version of GTK+ installed on your system (this includes the glib dynamic libraries) from http://www.gtk.org/ installed in the standard places, and that the makefile properly locates it. If you’re not sure, look up the -L command-line argument to g++ to learn how to explicitly add a path to it.

Good luck and have fun! If you learn Object Pascal you’ll understand C++ easily enough.

That’s good to know. I suspected they were equally capable.

The thing is, if C++ has the same syntax tree then surely it would be possible to make a C/C++ compiler that compiles just as fast as Object Pascal?

No I hadn’t seen that scripting thing. That’s exactly what I was talking about. Unfortunately I don’t have lots of time on my hands and it would probably take ages, given my meagre programming ability. Say average 500 lines of code per file for 600 files. Even if you translated one or two files per day, it would still take more than a year by which time the Blender code would have changed again.

Ah, GTK+ isn’t native on the Mac yet AFAIK. There are projects underway to make it so but they aren’t quite there yet:

http://gtk-osx.sourceforge.net/