<-- back

Object oriented Programming

OOP in NAL shall be single inheritance.

There is no need for a new keyword

Basic class/struct definition

NAL: // Before any module keyword, all declarations and definitions belong to the // global namespace. i32 .beersOnAWall = 99; // private global variable. // After the following line, all declarations and definitions belong to // the BaseType: module BaseType; f32 .drink(mutable this, f32 percentage); // pure virtual (semicolon) i32 .getBeerCount() { return beersOnAWall; } // static function. // After the following line, all declarations and definitions belong to // the Bottle (which inherits from BaseType): module Bottle : BaseType; f32 .volume = 1.f; // auto initialized value string .fluid; // uninitialized value bool .broken = false; // auto initialized value void init(mutable this, string .fluid) { // public method (without dot) fluid = .fluid; // dot to distinguish between member and parameter } own Bottle create(string fluid) { // public static function Bottle bottle(fluid); return bottle; } void destructor(mutable this) { // public destructor // do destructor stuff. } f32 getFillStatus(this) {return volume;} // const method. f32 drink(mutable this, f32 percentage) { BaseType.drink(percentage); // will cause error as pure virtual. volume *= 1. - percentage; return volume; } void dropOnTheFloor(mutable this) { broken = true; volume = 0.; } ... implementation part main (...) { Bottle bottle1("orange juice"); // using constructor Bottle bottle2 = Bottle.create("orange juice"); // using static method f32 fluidLeft = bottle1.drink(0.44f); }

Discussion

Extensions

All types shall be extensible by allowing the programmer to define Extensions and implement them. When implementing an extension inside of the implementation file where the type is declared, one has access to its private properties. Outside, only the public properties are available.

Advantage: An extension can be added at any place from anybody. It allows for rapid development of new features. A class which is not accessible privately can at least be molded into an interface which supports the methods of the extension. This is as easy as defining a method with a specific type for the this pointer:

NAL: ... extension if in the same file as definition of module Bottle: .refill(mutable Bottle this) { // this now refers to a Bottle if(broken) // access to private member printf("Cannot refill."); else volume = 1.f; // access to private member } ... implementation part { bottle = Bottle("IceTea"); bottle.drink(.5); bottle.refill(); }