Sather - A Language Tutorial

Ben Gomes, David Stoutamire, Boris Vaysman, Holger Klawitter

Edited by

Norbert Nemec

This file is part of the GNU Sather package. It is free software; you may redistribute and/or modify it under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; either version 2 of the license, or (at your option) any later version. *** This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See Doc/GPL for more details. *** The license text is also available from: Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


Table of Contents
1. Introduction
1.1. Acknowledgements
1.2. How to read this Document
1.3. Sources of Information
1.4. Obtaining the Compiler
1.4.1. How do I ask questions?
1.5. Summary of Features
1.5.1. Basic Concepts
1.5.2. Garbage Collection and Checking
1.5.3. No Implicit Calls
1.5.4. Separation of Subtyping and Code Inclusion
1.5.5. Iterators
1.5.6. Closures
1.5.7. Immutable and Reference Objects
1.5.8. IEEE Floating-Point
1.5.9. pSather
1.6. History
1.6.1. The Name
1.6.2. Sather's Antecedents
2. Classes and Objects
2.1. Preliminaries
2.1.1. Some basic classes - INT, FLT and STR
2.1.2. Printing output
2.1.3. Sather source files
2.1.4. Hello World
2.2. Defining Classes and Creating Objects
2.2.1. Defining Simple Classes
2.2.2. Checking whether an object has been created
2.2.3. Types Introduced
2.2.4. Hiding features: private and readonly
2.3. Class Data: shared and const
2.3.1. Shared Attributes - Restricted global variables
2.3.2. Class Constants
2.3.3. Accessing Class Data - the :: notation
2.4. Routine definitions
2.4.1. Routine Arguments and Modes
2.4.2. Local Variables - Scoping and Shadowing
2.4.3. Routine calls
2.4.4. Simple Overloading - Selecting a routine to call
2.5. Conditional Execution
2.5.1. if statements
2.5.2. case statements
2.5.3. Short circuit boolean expressions: and and or
2.6. Attribute Accessor Routines
2.6.1. Attribute assignment
2.7. Static Type Inference
2.7.1. Creation Expressions
2.7.2. Assignments and ::=
2.7.3. Arguments to a function call
2.8. Class Parameters
2.8.1. Arrays
2.9. A Running Example: Employees
3. Loops and Iterators
3.1. Using iterators
3.1.1. loop statements
3.1.2. Built-in iterators
3.2. Defining Iterators
3.2.1. yield statements
3.2.2. Explicitly leaving an iterator using quit
3.2.3. Control flow within an iterator
3.2.4. The once argument mode
3.2.5. out and inout argument modes
3.2.6. pre and post conditions in iterators
3.2.7. Argument evaluation in iterators
3.2.8. Points to note
3.3. Iterator Examples
4. Code Inclusion and Partial Classes
4.1. Include Clauses
4.1.1. Renaming
4.1.2. Multiple Inclusion
4.1.3. Resolving conflicts
4.2. Partial Classes and Stub routines
4.2.1. Mixins: A Prompt Example
5. Abstract Classes and Subtyping
5.1. Abstracting over Implementations
5.1.1. Implementing a Stack using an Array
5.1.2. A Stack Calculator
5.1.3. A Linked List Representation of a Stack
5.1.4. Switching Representations:Polymorphism
5.2. Abstract Class Definitions
5.3. Subtyping
5.3.1. The Type Graph
5.3.2. Dynamic Dispatch and Subtyping
5.4. Supertyping
5.4.1. Using supertyping
5.5. Type Conformance
5.5.1. Contravariant conformance
5.5.2. Subtyping = substitutability
5.6. The typecase statement
5.7. The Overloading Rule
5.7.1. Extending Overloading
5.7.2. Permissible overloading
5.7.3. Overloading as Statically resolved Multi-Methods
5.7.4. Conflicts when subtyping
5.7.5. Conflicts during code inclusion
5.7.6. Points to note
5.7.7. Overloading in Parametrized Classes
5.7.8. Why not use the return type to resolve conflicts?
5.8. When Covariance Ails You
5.8.1. But don't animals eat food?
5.8.2. Solution 1: Refactor the type hierarchy
5.8.3. Solution 2: Eliminate the offending method
5.8.4. Solution 3: Dynamically Determine the Type
5.8.5. Solution 4: Parametrize by the Argument Type
6. Parametrized Classes and Arrays
6.1. Parametrized concrete types
6.1.1. Why Parametrize?
6.2. Support for Arrays
6.2.1. Array Access
6.2.2. Array Classes: Including AREF and calling new();
6.2.3. Standard Arrays: ARRAY{T}
6.2.4. Multi-dimensional Arrays
6.3. Type Bounds
6.3.1. Why have typebounds?
6.3.2. Supertyping and Type Bounds
6.4. Parametrized Abstract Classes
6.5. Overloading
6.5.1. Overloading In the Parametrized Class Interface
6.5.2. Overloading Resolution within the Parametrized Class
7. Operator Redefinition
7.1. Method Names for Operators
7.2. Operator expressions
7.2.1. Operator precedence
7.3. Array Access Routines
8. Immutable Classes
8.1. Defining Immutable Classes
8.1.1. Immutable Class Example
8.1.2. Creating a new object
8.1.3. Initial value of immutable objects
8.1.4. Attribute access routines
8.1.5. Points to note
8.2. Using Immutable Classes
9. Closures
9.1. Creating and Calling Closures
9.1.1. Creating a closure
9.1.2. Calling a closure
9.1.3. Binding overloaded routines
9.1.4. Points to note
9.1.5. Binding some arguments
9.1.6. Leaving self unbound
9.2. Further Examples of Closures
9.2.1. Closures for Applicative Programming
9.2.2. Menu Structures
9.2.3. Iterator closures
10. Exceptions
10.1. Throwing and Catching Exceptions
10.1.1. Throwing Exceptions with raise
10.1.2. Catching Exceptions with protect
10.1.3. Usage to avoid
10.1.4. Alternatives to Exceptions
10.2. A more elaborate example
11. Safety Features
11.1. Preconditions
11.2. Postconditions
11.2.1. initial expressions
11.2.2. result expressions
11.2.3. Example
11.3. Assertions
11.3.1. assert statements
11.4. Invariants
11.4.1. The invariant routine
12. Built-in classes
12.1. Fundamental Classes
12.1.1. $OB
12.1.2. Array support
12.2. Tuples
12.3. The SYS Class
12.4. Object Finalization: $FINALIZE
12.5. Basic Classes and Literal Forms
12.5.1. Booleans and the BOOL class
12.5.2. Characters and the CHAR class
12.5.3. The string class, STR
12.5.4. Integers and the INT class
12.5.5. Infinite precision integers and the INTI class
12.5.6. Floating point numbers: the FLT and FLTD classes
12.6. Library Conventions
12.6.1. Object Identity
12.6.2. Nil and void
13. Interfacing with Fortran
13.1. Overview
13.1.1. External Fortran Call Example
13.1.2. Overall Organization
13.2. Name Binding
13.2.1. Difficulties
13.2.2. Implementation
13.3. Datatype Mapping
13.3.1. Scalar Types
13.3.2. Fortran Array Classes
13.3.3. F_ROUT and F_HANDLER Types
13.4. Parameter Passing
13.4.1. Return Types
13.4.2. Argument Types
13.4.3. OUT and INOUT Arguments
13.5. Portability Issues
13.5.1. Portability of the Interface Implementation Code
13.5.2. Portability of the Generated Code
14. Interfacing with ANSI C
14.1. Overall Organization
14.2. Built-in C classes
14.3. User-defined External C types
14.3.1. Constants and C binding names
14.3.2. Attributes and C structs
14.3.3. Shared Attributes and C globals
14.4. Parameter Passing
14.5. Inlining C Code
15. Statement and Expression Catalogue
15.1. Statements
15.1.1. Assignment statements
15.1.2. case statements
15.1.3. if statements
15.1.4. protect statements
15.1.5. loop statements
15.1.6. return statements
15.1.7. typecase statements
15.1.8. yield statements
15.1.9. quit statements
15.2. Expressions
15.2.1. void expressions
15.2.2. void test expressions
15.2.3. Short circuit boolean expressions: and and or
15.2.4. exception expressions