Object Pascal

Object Pascal
Paradigm Imperative, Structured, Object-oriented, Functional (Delphi dialect only), Component-based, Event-driven, Generic
Designed by Initially Apple Computer with input from Niklaus Wirth, and then by Borland International, led by Anders Hejlsberg[1]
First appeared 1986 (1986)
Typing discipline static and dynamic (dynamic typing through Variants, array of const and RTTI), strong, safe
Filename extensions .p, .pp, .pas
Major implementations
Delphi (x86, ARM), Free Pascal (x86, x86-64, PowerPC, ppc64, SPARC, MIPS and ARM), Oxygene (CLI, Java, Native Cocoa), Smart Mobile Studio (JavaScript)
Dialects
Apple, Turbo Pascal, Free Pascal (using objfpc or delphi mode), Delphi, Delphi.NET, Delphi Web Script, Oxygene
Influenced by
Pascal, Simula, Smalltalk
Influenced
C#, Genie, Java, Nim

Object Pascal refers to a branch of object-oriented derivatives of Pascal, mostly known as the primary programming language of Embarcadero Delphi.

Early history at Apple

Object Pascal is an extension of the Pascal language that was developed at Apple Computer by a team led by Larry Tesler in consultation with Niklaus Wirth, the inventor of Pascal. It is descended from an earlier object-oriented version of Pascal called Clascal, which was available on the Lisa computer.

Object Pascal was needed in order to support MacApp, an expandable Macintosh application framework that would now be called a class library. Object Pascal extensions and MacApp itself were developed by Barry Haynes, Ken Doyle, and Larry Rosenstein, and were tested by Dan Allen. Larry Tesler oversaw the project, which began very early in 1985 and became a product in 1986.

An Object Pascal extension was also implemented in the Think Pascal IDE. The IDE includes the compiler and an editor with Syntax highlighting and checking, a powerful debugger and a class library. Many developers preferred Think Pascal over Apple's implementation of Object Pascal because Think Pascal offered a tight integration of its tools. The development stopped after the 4.01 version because the company was bought by Symantec. The developers then left the project.

Apple dropped support for Object Pascal when they moved from Motorola 68K chips to IBM's PowerPC architecture in 1994. MacApp 3.0, for this platform, was re-written in C++.

Borland and CodeGear years

In 1986, Borland introduced similar extensions, also called Object Pascal, to the Turbo Pascal product for the Macintosh, and in 1989 for Turbo Pascal 5.5 for DOS. When Borland refocused from DOS to Windows in 1994, they created a successor to Turbo Pascal, called Delphi and introduced a new set of extensions to create what is now known as the Delphi language.

The development of Delphi started in 1993 and Delphi 1.0 was officially released in the United States on 14 February 1995. While code using the Turbo Pascal object model could still be compiled, Delphi featured a new syntax using the keyword class in preference to object, the Create constructor and a virtual Destroy destructor (and negating having to call the New and Dispose procedures), properties, method pointers, and some other things. These were inspired by the ISO working draft for object-oriented extensions, but many of the differences from Turbo Pascal's dialect (such as the draft's requirement that all methods be virtual) were ignored.

The Delphi language has continued to evolve over the years to support constructs such as dynamic arrays, generics and anonymous methods.

Versions

Compilers

Object Pascal compilers are available for a wide range of operating systems and architectures.

Legacy products

Interpreters

Main article: Pascal Script

Pascal Script (formerly known as InnerFuse) and DWScript (Delphi Web Script) are open-source Object Pascal interpreters/scripting engines written in Delphi. They support subsets of Object Pascal. DWScript can also compile Object Pascal code into JavaScript code (Smart Pascal), and supports just-in-time compilation.

Sample "Hello World" programs

Apple's Object Pascal

program ObjectPascalExample;

   type
      THelloWorld = object
         procedure Put;
      end;

   var
      HelloWorld: THelloWorld;

   procedure THelloWorld.Put;
   begin
      ShowMessage('Hello, World!');
   end;

begin
   New(HelloWorld);
   HelloWorld.Put;
   Dispose(HelloWorld);
end.

Turbo Pascal's Object Pascal

Still supported in Delphi and Free Pascal. FPC also packages its own substitutes for the libraries/units. Delphi doesn't. The Free Pascal 1.0 series and the FPC textmode IDE are the largest open codebases in this dialect. Free Pascal 2.0 was rewritten in a more Delphi-like dialect, and the textmode IDE and related frameworks (Free Vision) are the only parts in the TP version of Object Pascal.

Stack based allocation

program ObjectPascalExample;

   type
      THelloWorld = object
         procedure Put;
      end;

   procedure THelloWorld.Put;
   begin
      WriteLn('Hello, World!');
   end;

var
  HelloWorld: THelloWorld; { allocated on the stack and can be used without explicit allocation. }
begin
   HelloWorld.Put;
end.

Heap based allocation

program ObjectPascalExample;

   type
      PHelloWorld = ^THelloWorld;
      THelloWorld = object
         procedure Put;
      end;

   procedure THelloWorld.Put;
   begin
      WriteLn('Hello, World!');
   end;

var
  HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld }

begin
   New(HelloWorld);
   HelloWorld^.Put;
   Dispose(HelloWorld);
end.

Another example:

program ObjectPascalExample;

   type
      PHelloWorld = ^THelloWorld;
      THelloWorld = object
         procedure Put;
      end;

   procedure THelloWorld.Put;
   begin
      WriteLn('Hello, World!');
   end;

var
  HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld }
  HelloWorld2: ^THelloWorld; { this is a direct variable to a pointer of the THelloWorld type. 
                               This variable is type incompatible with PHelloWorld. } 
  HelloWorld3: ^THelloWorld; { the compiler sees this as being type incompatible with HelloWorld2 *and* 
                               PHelloWorld. Using the pointer syntax is the only way to declare a type that is 
                               assignment compatible. }
  HelloWorld4: PHelloWorld; { this is type compatible with HelloWorld, but incompatible with the other two variables. }
begin
   { This works in a similar way as the code above... but it demonstrates the disadvantage of not 
     using a specific type. The type definition between two variables using the raw pointer 
     based notation would be seen as "different" by the compiler. }

   New(HelloWorld4);
   HelloWorld := HelloWorld4; { this is valid - the type assignment is valid }
   HelloWorld2 = HelloWorld; { this would be a compiler error }
   {disposes left out for brevity}

   New(HelloWorld3);
   HelloWorld := HelloWorld3; { this would be a compiler error }
   HelloWorld2 = HelloWorld3; { this would be a compiler error }
   {disposes left out for brevity}
end.

This is an important distinction to make, and could be seen as one of the key differences that led to the Delphi style of "hiding" the heap allocation and pointer notation from the programmer. Delphi removed the need for the additional Pointer type and pointer syntax, but retained the explicit construction mechanism.

Delphi and Free Pascal's Object Pascal

program ObjectPascalExample;

type
  THelloWorld = class
    procedure Put;
  end;

procedure THelloWorld.Put;
begin
  Writeln('Hello, World!');
end;

var
  HelloWorld: THelloWorld;               { this is an implicit pointer }

begin
  HelloWorld := THelloWorld.Create;      { constructor returns a pointer to an object of type THelloWorld }
  HelloWorld.Put;
  HelloWorld.Free;                       { this line deallocates the THelloWorld object pointed to by HelloWorld }
end.

Note that the object construct is still available in Delphi and Free Pascal.

Oxygene Object Pascal

namespace ObjectPascalExample;

   interface

   type
      ConsoleApp = class
         class method Main;
      end;

      THelloWorld = class
         method Put;
      end;

   implementation

   method THelloWorld.Put;
   begin
      Console.WriteLine('Hello, World!');
   end;

   class method ConsoleApp.Main;
   begin
      var HelloWorld := new THelloWorld;
      HelloWorld.Put;
   end;

end.

DWScript's Object Pascal (aka Smart Pascal)

type
   THelloWorld = class
      procedure Put;
      begin
         PrintLn('Hello, World!');
      end
   end;

var HelloWorld := THelloWorld.Create;

HelloWorld.Put;

Note that the method implementation can also be made in a distinct location as in other Object Pascal dialects.

Development

Many features have been introduced continuously to Object Pascal with extensions to Delphi and extensions to FreePascal. In reaction to criticism, Free Pascal has adopted generics with the same syntax as Delphi, provided Delphi compatibility mode is selected, and both Delphi (partially) and Free Pascal (more extensive) support operator overloading. Delphi has also introduced many other features since version 7[5] including generics. Whereas FreePascal tries to be compatible to Delphi in Delphi compatibility mode, it also traditionally introduced many new features to the language that are not always available in Delphi.

See also

References

  1. Gibson, Steve (May 8, 1989). "Borland and Microsoft Enter the Object-Oriented Pascal Ring". Infoworld. p. 28.
  2. Community Letter: Embarcadero Technologies agrees to acquire CodeGear from Borland. Edn.embarcadero.com. Retrieved on 2013-07-21.
  3. "iPhone/iPod development". Free Pascal development team. Retrieved 2009-04-13.
  4. "A Smart Book".
  5. "New Delphi language features since Delphi 7". CodeGear. Retrieved 2008-07-06.
This article is issued from Wikipedia - version of the 10/23/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.