Saturday, May 14, 2005

Just an implementation detail...

One of my favorite features in Freya is the syntax of compiling units. Freya was initially inspired in Delphi, so it inherited what I call the “split organization”: declarations and implementations belong to different sections. Every compiling unit in Delphi has just an interface and an implementation section. Freya features a mutation of this structure. Every file in Freya is divided into one or more declaration sections, and zero or more implementation sections. Declaration sections are very similar to interface sections in Delphi… except that Freya doesn’t use the interface keyword. Declaration sections always start with public or private, and each section may contain several type declarations.
Implementation sections deviate more from the Delphi’s way. Each implementation section is associated to a single type declaration. Thus, all methods from the same type are always grouped together.
namespace Freya.Compiler;
Class01 = ...
Class02 = ...

implementation for Class01 is

implementation for Class02 is

Besides forcing a better grouping of method bodies, there’s another immediate advantage in this organization: you don’t need to repeat the type name in the headers of method implementations, as in Delphi and C++:
implementation for Class01 is

method Method01: string; ...

iterator Items: WhatEver; ...
But this is only the beginning. This syntax allows what I call the “just an implementation detail” attitude. Let’s say you need a private field for Class01. Why should you include its declaration in the class declaration? It’s just an implementation detail, after all! Consequently, Freya allows you to declare the field in the implementation section of Class01:
implementation for Class01 is

var Field01: Double;

method Method01: string ...
This trick can be repeated with constants, methods, constructors and even properties. The rule is: whenever you find a member in an implementation section that hasn’t be declared with the class, the compiler will consider it as a private member of that class.
More? In a previous post, I demonstrated interface implementation by delegation:
Class02 = class(IEnumerable);

implementation for Class02 is

var strings: array of string =
['Freya', 'rules!'];

interface IEnumerable = strings;

The implementation of an interface is a private business, so its place is the implementation section for the class. What about explicit method implementations, as in C#? Another implementation detail! You don’t have to declare the method in the class declaration (actually, you can’t):
implementation for Class02 is

method IEnumerable.GetEnumerator: IEnumerator;
By the way, there’s an interesting link between how we designed this Freya feature and Quenya! But that’s a different story, and it deserves another post...

Labels: , ,


Post a Comment

<< Home