Namespaces contain data types, and some data types, such as classes and records, define executable members, requiring an implementation.
Freya provides two different techniques for implementing executable members from class and records: a separate implementation section for each type, and declaring that implementation section along with the corresponding type. This example shows a source files defining two classes, each one with a separate impementation section:
Note |
---|
This is a rather lengthy example, and it's not the recommended writing style for Freya. For classes as simple as Counter and Message, it's better to provide an inline implementation, as shown below in this same topic. The Value property, from Counter, has an explicit implementation, but it could have an automatic field-based implementation. Finally, private fields, such as fValue, can be declared directly inside the corresponding implementation section. |
namespace Demo; public Counter = class private fValue: Integer; public property Value: Integer; readonly; method Increment; method Decrement; end; Message = class public static method Show(msg: System.String); end; implementation for Counter is property Value: Integer; begin Result := fValue; end; method Increment; begin fValue++; end; method Increment; begin fValue--; end; implementation for Message is method Show(msg: System.String); begin System.Console.WriteLine(msg); end; end.
Some remarks:
There are some advantages when using independent implementation for sections. For instance, they provide a simple method for separating public details from implementation concerns. However, when used with small classes, they can be cumbersome: you may end typing a lot of unnecesary text.
For small classes and records, it's better to provide an inline implementation, writing the implementation section inside the declaring type:
namespace Demo; public Counter = class public property Value: Integer; readonly; method Increment; method Decrement; implementation method Increment; begin Value++; end; method Increment; begin Value--; end; end; Message = class public static method Show(msg: System.String); implementation for Message is method Show(msg: System.String); begin System.Console.WriteLine(msg); end; end; end.
There are no great savings in space, but now you can read all the code related with a given type without being disturbed by other declarations. Please note that, this time, we have implemented Value as a field-based property... just by omitting its implementation. Thanks to this technique, you can use Value from inside its class as if it were a field.
We can save even more space by merging member declarations with their implementations. For example:
namespace Demo; public Counter = class public property Value: Integer; readonly; method Increment; begin Value++; end; method Decrement; begin Value--; end; end; Message = class public static method Show(msg: System.String); begin System.Console.WriteLine(msg); end; end; end.
This programming style is similar to those from C#, Java or Eiffel. It's up to you to decide which syntax fits better to your requirements and goals.
Note |
---|
Implementation sections are still required for some special members, such as static constructors and interface delegations. |
The Freya Programming Language
Program and file structure
Lexical structure
Projects and source files
Namespaces
using clauses
The entry point
Type declarations
Type members
Statements