Language

From The Oxygene Language Wiki

Jump to:navigation, search

This is the Oxygene Language area
Areas can only be created/modified by sysops


NOTE: This is article is a work in progress and not finished yet.
Unless you are the author, please do not make changes or rely yet on information presented in this article.

When done, the author should change the first line from wip to wipr to show that the page is ready for review.

If you have suggestions for this page, please send them to us: email.

Oxygene is a modern, Object Pascal-based language designed to develop applications for today's managed platforms, .NET/Mono and Java/Android. It is a purely object oriented language, meaning that all code is contained in Classes, which can expose members such as Methods, Properties and Events.

Keywords

Object Pascal, just like Pascal before it, is easily readable by both humans and computers. This is by design, and is accomplished through the use of English words instead of symbols to define the language. For example blocks of code are delimited with the BEGIN and END keywords. Compare this to the C family of languages that use the curly braces { & } to accomplish the same task.

A great example of the use of English keywords is with the conditional IF statement. Based on the evaluation of one or more values, the IF statement allows different blocks of code to be executed. The syntax is IF value THEN operation. When multiple values must be true, then the Boolean operator is AND, which produces syntax like IF value AND value THEN operation.

Case & Naming Conventions

By convention, reserved words in Pascal were originally in all uppercase. The language however is not case sensitive. With the advent of syntax highlighting IDE’s reserved words are now typically lowercase by convention. In Turbo Pascal and Delphi it was convention to prefix class names with a T. This is no longer the convention with Oxygene. There are no formal naming or case conventions in Oxygene. Typically the naming and case follows the conventions of the underlying framework.

Structured Programming

One of the things that differentiated Pascal when it was introduced is that it was designed to be a structured programming language. This translates into two things: The first is that it was designed to support complex data structures like lists and records. The second is that it supports various control structures like FOR, CASE, WHILE, REPEAT, etc. as well as procedures & functions without the need to rely on GOTO or JMP like unstructured languages.

Procedural Programming

In its initial incarnation Pascal was a procedural language. This was another advancement over BASIC and other languages that made use of GOTO or GOSUB and similar code navigation mechanisms. As a procedural language this allowed the programmer to create a reusable routine that could accept and return values.

Pascal originally differentiated a PROCEDURE from a FUNCTION (a procedure that returned a result) by different identifiers. The Oxygene language no longer differentiates the two by keyword and simply uses the METHOD keyword for both.

Object Oriented Programming

One of the big differentiators of Oxygene from Pascal is that Oxygene is purely Object Orientated. What this means is that in Pascal you could declare a procedure anywhere and all data shared between procedures was globally accessible, while in Oxygene all methods (aka Procedures) and data (variables) are declared in a class. Then all data and methods are accessed via instances of classes, which are objects. Oxygene does not support declaring methods or variables outside of a class.

There are a number of advantages to object oriented programming. The most noticeable advantage is organization of related methods and the data it works with. This organization leads to encapsulation, which is a class’ ability to hide the access to some data and methods from use outside the class. The hiding of complexity makes it easier for programmers to reuse complex libraries of code.

Structure of Code

All the code in an Oxygene project is divided into code files. Each code file can only contain types from one namespace. The namespace is specified at the top of the file. A namespace can however span multiple files, or even multiple projects.

namespace MyNamespace1

After the namespace the code file is divided into two parts, the interface and the implementation. The interface section is similar to the separate header file found in many languages from the C family. It defines the public interface of the code found in that code file. The advantage of the interface section is it provides a convenient human and computer readable summary of the classes in the code file. This speeds up human navigation and comprehension of the code as well as compilation time.

The implementation section is the details of the code file. This is the location of the code that implements the classes defined in the interface section. A class could be defined in the implementation section alone, but it would not be accessible from other code files. The implementation section provides a level of encapsulation of the complexity of the implemented code.

By convention it is common for only one class to be defined per code file. There are times however where a single class is defined in multiple code files by use of partial class support.

Namespaces provide a way to further group related classes. Namespaces also prevent naming collisions for different classes that have the same name. For example, if there are two Bar classes, one can be in the Foo namespace, while the other is in the Gold namespace. Any code in the Foo namespace will reference the Foo.Bar class by default when just referring to it as Bar. If that code needs to refer to the other Bar class then it will need to refer to it as Gold.Bar.

The uses clause may be present in the interface or implementation section (or both). It provides a way to shortcut references to classes in different namespaces. If code is written in the MyProject namespace, and it will need to reference code in the Foo namespace then it can include the uses Foo clause and all references to any class will first be looked for in the MyProject namespace. If it is not found there then it will look for it in the Foo namespace. If a reference needs to be made to the Bar class in the Gold namespace then it will need to reference it as Gold.Bar.

namespace MyProject; interface uses Foo; type

 MyClass = class
 public
   method MyMethod;
 end;

implementation method MyClass.MyMethod; begin

 // Do something

end; end.

The end of every code file is indicated with the end keyword followed by a period.

Classes

Classes defined in Oxygene belong to a class hierarchy rooted in a base object called "Object" (System.Object in .NET and java.Object in Java). Every class either defines an explicit ancestor type, or directly descends from Object. Classes inherit all members from their ancestor classes and are thus said to extend that class – by adding additional members or providing overridden implementations of select members defined in the ancestors.

Class members can be declared at different visibility levels, to control how they can be accessed. The three most important levels include "private" visibility that restricts access to the class itself, "protected" visibility that allows access only from descendants of the class, and "public" which allows any code in the project to access the member.

Classes can me marked as abstract to indicate that they need to be extended in order to be instantiated and used. They can also be marked as sealed to indicate that they can not be extended further.

Methods define the execution flow of applications written in Oxygene. Each method consists of a list of Statements that get executed in turn, as a method is called. Statements types include loops, conditional statements, logical and mathematical expressions or calculations, or calls to other members in the same or different classes.

Properties provide controlled access to data contained within a class instance, and can be read/write, read-only or (in rare cases) write-only. Events allow objects to retain references to call-back methods on other objects that they can call, when necessary.

Other Types

The Oxygene Language also provides support for Interfaces. Interfaces are abstract types that define a set of shared methods, events or properties that can be implemented by one or more (otherwise unrelated) classes. Classes that implement the same interface can be accessed in identical ways by code that might otherwise be unaware of the concrete class types.

To partition code and avoid name conflicts, all types are considered to belong to a Namespace. Namespaces allow different types with the same name to exist in different portions of the framework or your own code, and make it easy to determine what framework or project a particular type belongs to.

To complement classes, the language offers additional types such as Enums (which are collections of named values), Records (which behave similar to classes, but are stack based), as well as traditional primitive types for strings and numbers.

Advanced Concepts

The Oxygene language contains many advanced language concepts, some of which are inspired by other, less mainstream languages and others which are entirely unique to Oxygene.



See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox