# trygve
an overview
James O. Coplien
Winter 2022
# 1. The What and Why of **trygve**
Programming is always about producing illusions through a reality that the programmer creates in a computer and between a computer and a human being. We can profitably use some of these illusions, but many of them must fight through layers of history and prejudice to touch anything deep inside the end user, or her surrogate, the "programmer." Much of what we know about programming gets in the way of engagement between end users and the objects with which they playfully interact to achieve some task. For example, while end users think in objects, we script execution in classes. While end users think in scenarios and problem-solving approaches, we focus on individual methods and on the state of individual objects.
To get the most enjoyment from this tutorial reference you might best willfully suspend your disbelief and forget much of what you think you know about programming. The **trygve** (pronounced ˌtɾyɡ' ʋə) language is in some sense designed for a child-like mentality that hasn't yet been polluted by computer science, but which retains problem-solving skills that children exercise through eyes that see the world in a less stilted way than a programmer does. The approach took its original inspiration from what we can glean from Alan Kay's early ideas of how his vision of the Dynabook computer could aid children in what we can refer to in vulgar terms as _learning_, but which entails much more, to the point of thinking of the machine as an extension of self.
The **trygve** language allows you to think about your program in terms of mental models of some phenomenon that you bring to the table. It's called object-oriented programming, so we're going to talk about classes as a supporting cast rather than as the star players. Here, objects are the stars of the show. We talk about objects in terms of the names our compressed mental models give them as they interact to solve some problem. Those names are Roles. Any given Role is just a name but at the same time is much more. The Role _fireman_ is just a name for some human being in a particular Context, but it also elicits a host of associated behaviors. Those behaviors that are germane to the Role itself, without much regard for the Role-player, are called _scripts_ in **trygve** (computer scientists call them _methods_) — little recipes for doing very small tasks.
While Roles link to the left brain, classes link to the right brain. We filter billions of bits of information in the world around us to make sense of it, and our massively parallel right brain is really good at doing that. We use classification to compress the common aspects of many related objects into one concept. Though we see Hamlet a 100 times we know just a single script. We may start by organizing our world into classes, which will provide the basic building blocks of local state and ways of interacting with that state.
Programming becomes an act of script-writing. A "program' in the sense that we usually use the term, for the source code, isn't the real program at all — it's just the script that the enactment will follow. The **trygve** language is designed to reduce the transition from run-time to scripting and back again. The "programmer's" mental model should be focused on the action and, in particular, on the interaction between the end-user and the objects which represent that user's interests inside the machine.
The **trygve** language syntax takes a cue from Java syntax mainly for reasons of broad familiarity. With the Java legacy comes a legacy of arithmetic types — to a Martian, it would seem that all programmers are working in the domain of mathematics. The fantasies of academia aside, it simply isn't so. Very few computers compute — i.e., very few of them produce an "answer" or "result." They run machinery, fly planes, process telephone calls, with any of the numbers of those domains playing only an incidental part in the structure of the program.
But **trygve** departs from Java in many subtle ways. You can write Java in any programming language, and **trygve** is very accommodating for those who prefer Java syntax. But experience with the language will help you discover that it has a few features that make it feel at times like Erlang, and at times like Ruby, or maybe some other language that has graced your programming history.
And **trygve** departs from Java in yet more fundamental ways. Java tried to be a pure OO language by outlawing global functions, but that is a simplistic hope at best. It ended being only a class-oriented programming language. Like most languages of its kind it has many features to finesse class relationships. These features encourage class-oriented thinking, overuse of inheritance, and programmer convenience over end-user mental models. So you will find neither friends, or static objects, or the concept of super, nor the protected access property in **trygve**.
The **trygve** language is just one part of a system design that supports end user mental models. In the end, **trygve**'s main contribution is to the left-brained side of computation — the enactment of scripts. Users still engage their right brain during program enactment but in modern computing, such activity is usually associated with the visual cortex. Identifying the right entities (objects) happens on the screen, and Model-View Controller (MVC) has been designed as the bridge between end user and computer in that regard. MVC and **trygve** can powerfully be combined to provide the most expressive links between the end user and the machine.
# 2. Enactment Building Blocks
The **trygve** language, as a language, has its writings appear on two-dimensional surfaces such as your computer screen or a sheet of paper. We usually call this sequence of characters a program. In **trygve** we call it a _script_, to evoke a metaphor of theatre with a lot going on in the framework of defined Roles. The **trygve** environment is designed to help you think about what exists and happens during the enactment of the script — the actual performance of the play. The program that you write in **trygve** is a set of instructions for bringing about a certain enactment. The script describes what can happen at run time and, in many instances, describes the ordering constraints on run-time activities.
The main run-time building blocks are called _objects_. Each object works with other objects to solve some business problem together. We adopt this model in contrast to letting one method use the objects and their services as slaves, orchestrating them to achieve some outcome. We instead push the interaction control into the objects. Nonetheless, we maintain the code for these slices of control in one administrative entity called a Context, on a per-use-case basis.
Objects exist only during script enactment (in computing terms, when we are "running the program"). Each one carries out the work that it "knows" how to do, and the sum of all the small activities from individual objects add up to a fully performing program.
These smaller activities that take place inside objects are small scripts in their own right so we call them scripts, too. In the programming vernacular they are called _methods_: a kind of prescription for a way of doing something. Here is a **trygve** script for computing the factorial of an integer n:
```java
int fact(int n) {
return if (n <= 1) 1 else n * fact(n-1)
}
```
Each object has the ability to run one of its scripts at a time. At a low level a script is a collection of expressions that are enacted in a specified order.
(Actually, if you use **trygve** primitives like `read` that cause the current execution of sequence to pause, another activity, such as pressing a key on a keyboard or moving the mouse, may enact another script on the same object. But that is the exceptional case. For more about this, see the documentation on `Panel` and particularly on the `handleEvent` script.)
Though **trygve** supports the illusion that each object has its own copy of its scripts, we need to script the activity only once — in the object's _class_. This is done before program enactment. It will be in classes, and their close cousins Context and Role, where most of the scripting activity takes place. But it's important always to keep in mind that a real program is a living, breathing thing, interacting with a (less metaphorically) living, breathing end user out there. The mindset during scripting should always be in terms of the objects in play and in terms of their interactions with the end user during program enactment.
So we now have the context to move on to scripting.
# 3. Script Building Blocks: Declarations and Expressions
As a problem-solver you will be writing scripts that bring the program and its enactment into being at run time. These scripts are the two-dimensional text you write on a piece of paper or type into the source window of the **trygve** environment. The **trygve** language is a textual language, mainly because we felt that, for whatever reason, textually based programming languages seem to thrive while graphical programming languages founder or never gain any footing. The most likely reason is that we simply don't yet know how to express complex solutions well in graphical form. Text can be highly expressive, even with a limited vocabulary, because of the meaning that is generated by the sequential arrangement of textual content on a page.
Scripts in **trygve** (the **trygve** source language constructs) have two basic building blocks:
- Declarations
- Expressions
There is no concept of 'statement' in **trygve**. _Declarations_ are the foundation and the scaffolding for your program, and _expressions_ are the things you write to instruct your program how to do stuff. Declarations include, for example:
- Classes — A class specifies the makeup of an object and is the place where we write the basic scripts to interact with the state of that object. We say that an object is an _instance_ of its class. The class captures the properties of some concept from our business, where that object corresponds to something we name, manipulate, or manage in the real world. We can use a class to describe the concept of a rectangle in the domain of geometry:
```java
class Rectangle {
public double area() { return height_ * width_; }
public Point center() {
return new Point(upperLeft_.x() + lowerRight_.x() / 2,
upperLeft_.y() + lowerRight_.y() / 2);
}
. . . .
Point upperLeft_, lowerRight_;
double height_, width_;
}
```
This class happens to contain the scripts `area` and `center`, and also includes declarations for the data `height_` and `width_`. Data are how we remember stuff. Data encode information that is important to support our problem-solving; how they encode it should be of no concern to the user of the enclosing object. The representation, or encoding, of information as data is encapsulated inside of a class, and the users of the corresponding information interact with the data through the _scripts_ of that class. In the above example, Rectangle is a class — but so are Point and double. We access them through those declarations that it chooses to make public. By the way, a class exists in a condensed and largely hidden form during program enactment.
- Contexts — a Context is a script for a complex set of interactions involving several objects. The overall script is broken down into individual scripts called _Roles_. A given set of objects may play a given set of Roles to enact some system-level script. In computerese we call this higher-level script a _system operation_; the requirements people call it a _system_ _use case_. This level of enactment is akin to a scene in a play. Like a scene in a play, a given Context enactment has a beginning and an end (though in theory a given system operation — or a play — could go on forever).
- Roles — A Role is the script for an object participating in a particular system operation or 'scene.' Roles exist only inside Context scripts. Each object participating in a given Context enactment (system operation) 'knows' how to play its Role. Role-playing knowledge is bestowed on an object when it becomes engaged in a Context script, and it forgets all of that Role-playing ability at the close of the Context action. A Role comprises _scripts_ which an object enacts when receiving a cue from another Role; that object in turn can elect to send cues to one or more objects that are also playing Roles within the same Context. An object playing a Role is called that Role's _role-player_.
- Scripts are like the lines spoken by a given role-player. A script is a collection of expressions, and possibly supporting declarations, that describe how it carries out its part of a dialogue that is elicited by a cue from another object. Most Roles have multiple scripts, just as a Role in a play may speak and do several things in a given scene of a play.
Declarations may use expressions, as well as other declarations, as building blocks. (The same is true for expressions in that they may use declarations as well as other expressions as building blocks.)
Expressions include, for example,
- Arithmetic and boolean computations (addition, comparison, etc.)
- Cueing another object (the way we script interaction between objects)
A **trygve** program comprises a list of declarations followed by a single expression (though that expression may be quite elaborate). Enactment starts with the evaluation of that expression. Of course, that expression interacts with the preceding declarations, and a large, complex enactment may unfold from these interactions.


# 4. Scripts
You write a script to orchestrate the solving of your business problem. A script is a sequence of Declarations followed by a single Expression that kicks things off — usually by interacting with the expressions in the Declarations. A very simple program may not have any programmer-supplied declarations at all but only an expression. So this is a perfectly good, complete **trygve** program:
```java
System.out.println("Hello World")
```
This expression uses a built-in declaration of class System that lives inside the **trygve** environment, which publishes an interface to an object called out that programs can use to send output to the user. The object can be asked (we always _ask_ objects — never _tell_ them) to evaluate the expressions of its script called `println`. We send along some information in our cue to the `out` object requesting the performance of the built-in `println` script, which is a string that we want `out` to print: 'Hello World'. And `out` will do just that.
Start up the **trygve** environment on your machine. Copy the above script into the text pad (the editing area on the left half of the screen). You can either copy and paste the text or simply type it into the text pad. Or you can select 'Example Text' from the **Edit** menu.
Next, press the **Parse** button. This causes the **trygve** environment to read the script and to "understand" what it is supposed to do. During this process **trygve** will look for errors in your script: it's pretty fussy about the grammar you use. If all is well **trygve** will draw a blue line beneath the welcome message.
Last, touch **Run**. This will cause **trygve** to evaluate the last (and only) expression in your script. The result is that the message 'Hello World' appears on the output pad.

`System.out.println` is one kind of expression. Like all expressions, it can be combined with other expressions into a larger script. Declarations can also be used to group together scripts related to some real-world object, or to group together scripts for a given Role.
## 4.1 Mathematical Expressions
Mathematicians were some of the first programmers and, for better or worse, most contemporary programming languages bear their mark. I remember that when I started studying ancient Hebrew that our instructor advised us that there is really nothing magical about the Hebrew language and that, contrary to belief of some, if wasn't the language spoken by the Deity or anything like that. Computer scientists tend to have the same belief about mathematics: that it is somehow fundamental to what computers were designed to do. In fact, few computers 'compute' (i.e., evaluate formal mathematical expressions to give a certain number or set as a result), but rather serve as a way to extend the user's processing of information. Numeric data have proven themselves over history as a convenient way to represent information. So we might represent a given color as 164 parts red, 210 parts green and 237 parts blue (which represents the color of part of the shape in the header of this page). Since it is useful to have a single common underlying infrastructure that is common across the industry (because of the economies of scale and scope that come with building on a single technology rather than trying to support several technologies in parallel), numbers have found a home in computing. Numbers form the alphabet from which we craft the "words" and, ultimately, the expressions of computation. But we really just use numbers and rarely use mathematics. Mathematics is a language for talking about numbers and, by inference, real-world things that we choose to represent by numbers (such as the size of your paycheck). Here we are not using the language of mathematics, but rather a language called **trygve**. One might use the language of mathematics to talk about **trygve** programs (e.g., to prove that they have certain properties) but we won't do that here.
This is just a long way of saying that 1. We can't avoid dealing with numbers and arithmetic, and 2. There are many **trygve** expressions to talk about ideas from arithmetic. So `1 + 2` is a valid **trygve** expression. As an expression, we can combine it together with our earlier println expression to say:
```java
System.out.println(1 + 2)
```
which of course prints 3. The common arithmetic operators are all there: –, *, +, and /. There are also less common binary operators like ** for exponentiation (e.g., 2 ** 30 yields 1073741824) and % for modulus.
You can use parenthesis to group smaller expressions into larger expressions. For example, the expression
```java
(-b + sqrt(b ** 2 – (4.0 * a * c)) ) / (a + a)
```
might be used to find one of the roots of a simple polynomial. The expression `b ** 2` means "raise b to the second power;" `4 * a * c` means to multiply 4 by a, and they to multiply the result by c. Evaluation normally proceeds from left to right (unless your use of parentheses groups expressions differently).
## 4.2 Identifiers
But what are `a`, `b`, and `c`? Expression like the one above became familiar to you if you graduated from arithmetic to algebra. In algebra, we use letters to represent some value that can vary every time we use any given expression. Algebra calls them _variables_. In **trygve** we call them _object identifiers_, or just _identifiers_ for short.
Mathematicians tend to use letters of the alphabet to name numbers (and they'll sometimes use a variety of alphabets depending on their goal, so you may know, for example). We find single letters a bit boring, so we use ordinary English (or pick your own language: the native **trygve** language is Nordic) words as identifier names. So we might more clearly describe the quadratic formula like this:
```java
(-coefficientOfSecondOrderTerm + sqrt(coefficientOfFirstOrderTerm ** 2 –
(4.0 * coefficientOfFirstOrderTerm * constant)) ) /
(coefficientOfFirstOrderTerm + coefficientOfFirstOrderTerm )
```
This looks long and you probably are thinking that it's too much typing. However, programs are read dozens of time for each time you write them, and we want code to be above all understandable, even by those who didn't write the code.
An identifier is a name for an object. Identifiers behave like PostIt® notes that can be affixed to objects. We associate an identifier with an object like this:
```java
a = 1
```
This creates a PostIt note with '`a`' written on it and affixes it to the object for which _1_ is the symbol. _1_ is just a symbol in the **trygve** language that represents a very simple mathematical object. _2_ is another symbol that represents another object. These two objects are similar in many ways: they can be added and multiplied in the same ways, and the 'things we can do to them' are the same (all the abelian operators). These objects belong with each other, and many others, as members of a set: the set of _integers_.
By the way, if we say:
```java
b = a
````
then both `b` and `a` end being names for the same object. If we then say:
```java
b = 2
````
then `b` ceases to be a name for the object represented by the symbol _1_ and becomes a name for the object for which _2_ is the symbol. The identifier `a` persists as a name for the object representing the integer _1_.
If you want to know whether two identifiers name the same object, you can ask:
```java
if (b is a) System.out.println("b and a are the same object")
```
This is a different thing than asking whether two objects have "the same value." We write that like this:
```java
if (b == a) System.out.println("b and a have the same value")
```
What "have the same value" means varies from type to type and usually matches intuition. If we wrote:
```java
a = 1
b = 1
```
most people would agree that `a` and `b` have the same value; however, the are _not_ the same object. That is:
```java
int a, b
a = 1, b = 1
System.out.println(a == b)
System.out.println(a != b)
System.out.println(a is b)
System.out.println(a is not b)
```
will print:
```java
true
false
false
true
```
Compare that with:
```java
int a, b
a = 1, b = a
System.out.println(a == b)
System.out.println(a != b)
System.out.println(a is b)
System.out.println(a is not b)
```
which will print:
```java
true
false
true
false
```
Most identifiers start being associated with the value null when you declare them but before you initialize them or assign to them. You can check if an identifier has been bound to an object with the expression:
```java
objectName is null
```
and you can associate a name with the null value by saying
```java
objectName = null
```
The expression
```java
objectName == null
```
is also meaningful and gives just about the same result as the expression `objectName is null`.
Expressions become much more powerful when we can write them in terms of identifiers. We'll learn more about identifiers in the next section.
The rest of this paragraph is probably a bit more than most people want or need to know, so you skip ahead to 4.3 if you like. There are longstanding traditions in programming, most notably those that spring from the BCPL-, B- and C- based cultures and from UN*X programming, that leading underscores are reserved for internal and system use. It long predates C++ and is much broader than just C. The same sentiment holds broadly for most Microsoft software and the use of leading underscores is explicitly discouraged in the CLS. It's outright illegal in Delphi (yes, there are still a lot of Delphi programmers, particularly in the Balkans).
I don't want **trygve** code to look too much like computer code — at least to the extent that is possible within Java confines.
There are tougher restrictions on **trygve** identifier names that are somewhat more conventional. Many internal **trygve** variables use embedded '$' characters. In one case, I use a leading space (the name of the class from which Class is instantiated starts with a space, as I recall). The lexical analyzer doesn't accept any of these so it's impossible to alias internal program names.
There are other restrictions on naming, but that's an Easter egg and I'm going to leave it to others to discover. No fair looking at the code!
## 4.3 Types and Simple Declarations
The **trygve** language knows about a small number of sets, including the set of integers. We have a name for this set, which is `Integer`. (We can also use the shorter name `int` for historical reasons, and that is the preferred usage). Instead of using the word 'set' when we talk about **trygve** programs, we use the word _type_. This word (in English) emphasizes that its members share similar characteristics. We use types to constrain the kinds of objects that identifiers can associate with, like this:
```java
Integer a, b
```
`Integer` is a type. This code tells **trygve** that the identifiers a and b may be associated only with `Integer` objects. This helps **trygve** help you keep things straight in a program. The **trygve** language also understands the additional small set of types `String`, `double`, and `boolean`, and a few others. Some of them are described in the appendices.
The above line of code is called a _declaration_. Declarations join _expressions_ as one of the major building blocks of **trygve** scripts. By itself, a simple declaration doesn't do any work or run any script. It doesn't create any objects. It takes no memory in the machine. It merely "declares" some properties of its identifiers, for future use. You must declare an identifier before using it: Within a script the declaration for an identifier _usually_ must appear before its use. In general, it's a good idea always to have the declaration for an identifier appear above its use within a script. Longstanding tradition holds that the declarations within classes (and analogous declarations like Contexts and Roles) put the public, published declarations at the top, because those are of most interest to the common reader of the code. However the **trygve** language is flexible for some declarations and may allow them to come out of order. We'll see some examples later.
## 4.4 Objects
Objects are the key building blocks of a running **trygve** program. The **trygve** scripts bring objects into existence and the objects can have access to each other, to invoke each other's scripts, to achieve some overall business goal.
For you computer scientists: you're probably thinking that I misspoke about _1_ when I used the word _object_, because most programming languages distinguish between objects and values. The **trygve** language does not cling to any such distinction because, for all practical purposes, it deals only with objects. There is only one value in the **trygve** language, which is `null`, and we'll talk more about it later. In short, it is the object that is no object.
Objects are agents inside our program that can do basic, simple, local computations. They are like individual actors that we hire if we are producing a play. Individually an actor may not be able to do much that is impressive, but several actors exchanging lines and working in concert can rise to complex action.
An object has three distinguishing properties. The first is behavior. An object is a locus of related behaviors that together model the behaviors of a concept that is part of our model of the world. So an object in our program helps us use the computer to extend the processing of our brain according to how we program it. We implement an object's behaviors as a collection of scripts. Each script has a name and a few other properties that help us to use it in a flexible way.
The second is state. When a script executes in an object it usually changes the object state. For example, if we have an object that represents a triangle on a plane, and the object executes its `moveTo` script, the object's location changes. If the object is charged with 'knowing' its location we say that location is part of its state. State has meaning only under the possibility of change. An object's state is usually implemented in a specific small collection of computer data that are allocated when an object comes into existence.
The third is identity. If an object exists and we associate an identifier with it, and then associate another identifier with the same object, then we can send a cue to that object through either of those identifiers and the end result will be the same.
When the symbol 1 appears in a **trygve** program, the program creates a new object representing the integer value 1. So the statement:
```java
a = 1
```
first creates an object representing the object 1, and then associates it with the identifier `a`.
## 4.5 Classes, Scripts and Cues
We have talked about the type `Integer`. `Integer` has its own predefined scripts for adding, subtracting, multiplying, and dividing numbers, and maybe a few other arithmetic tasks. Each `Integer` has its own data to store its state. Someone has written the code for `Integer`. The construct that holds that code is called a _class_.
A _class_ is a concrete implementation of a type. Classes are the most common building blocks of **trygve** scripts. We think objects and write classes and Roles. A class looks like this:
```java
class ClassMate {
public String familyName() {
return familyName_;
}
public String firstName() {
return firstName_;
}
public String nickName() {
return nickName_;
}
public int age() {
Date today = new Date();
return today.getYear() – birthYear_;
}
private int birthYear_;
private String familyName_, firstName_, nickname_;
}
```
We usually capitalize class names. This simple class represents one of any of our classmates. Its scripts so far are very simple, just giving us some insight into the data used to represent the information in the class's state. Good classes are simple — and dumb.
A class is the DNA of an object. If an object is like an actor, the class is what gives it is basic, simple functionality. All new objects are created with the 'DNA' of their class. That DNA includes the general structure of the object as well as all of its scripts.
### 4.5.1 State declarations
Before talking about the class's scripts, let's look at its description of _state_. The class has declared three identifiers: `birthYear_`, `familyName_`, and `firstName_`. These identifiers can each be associated with an object. (In a bit we'll describe a convenient way to set up the associations between these identifiers and their objects). Note that each name ends with an underscore: a convenient convention. Each declaration gives its identifier a type. In this case all the identifiers are declared with a `private` attribute. That means that they are private to `ClassMate`. They may be accessed only by the scripts inside of `ClassMate`: as far as any external script is concerned, these identifiers don't exist.
The name attributes `familyName_`, `firstName_`, and `nickName_` are all names of objects of class `String`. A `String` holds a sequence of characters that we usually associate with text that can appear on a screen or on a printed page. Its contents are the closest we get to an object that can be "digested" by human senses (though we still need the machinery inside the computer to make the object visible on the screen or on the page — but that's just part of the machine, and it's the same for all programming languages). You can't read an `Integer` — it's just a mathematical concept, and the machine may choose to internally represent it in binary-coded decimal, in binary, or in a more efficient representation like ternary. But we should leave those matters to the machine. Therefore, like most other built-in types, `Integer` provides a way for us to convert it into something more digestible by humans: a `String`. We do this with the `toString` script inside `Integer`:
```java
Integer i = 123;
String s = i.toString();
System.out.println(s)
```
Note that if we just say:
```java
System.out.println(i)
```
we get the same result; that's because `println` automatically applies the `toString` script for us so it can display it on output. The `println` script does this for all built-in types. When you define your own types (we'll talk about that later) you should usually include a `toString` script as part of your implementation.
These lines declare and define the script `familyName`:
```java
public String familyName() {
return familyName_;
}
```
Altogether, this is another kind of declaration. This declaration describes a script (which doesn't do much). In computerese these are called _methods_. We declare the script as public, meaning that any other code that has access to an object of class `ClassMate` is allowed to ask that the script be run.
### 4.5.2 Script declarations
A script declaration typically starts with a `public` or `private` access modifier, followed by the type of the object that the script delivers back to the client when it is done. (If the script provides no value in return for the cue, then we use the type "void" here). Then comes the name of the script, followed by a pair of parenthesis whose importance will soon become apparent. Then comes the real script: the collection of expressions that the script will execute. These expressions are grouped together inside a pair of curly braces. The lines between any pair of curly braces together are called a _block_ of code. The main block for a script is called the script's _body_.
The `familyName` script belongs to the class `ClassMate`: we think of it as being replicated _inside_ of each `ClassMate` object. We think of every `ClassMate` object as having the ability to run the `familyName` script; because it's `public`, anyone with a handle on a `ClassMate` object can cue the `familyName` script to be enacted. While it is being enacted we think of the action happening inside the `ClassMate` object. Inside the object we find other scripts as well as identifiers that have become associated with other objects. We see the `familyName` script refer to the identifier `familyName_` (note the trailing underscore — a manually maintained convention for naming identifiers that belong to an object, and which are declared in those objects' class). Because the `familyName` script is "running inside the object" and because the identifier `familyName_` is also inside the object, the script can just casually invoke the identifier name to interact with the object for which it is a name. The same would be true for `birthYear_`. And the same would be true if one method wanted to cue another method within the same object.
Let's assume that some client of `ClassMate` has become a client by somehow receiving an object of class `ClassMate`. It associates that object with the identifier `studyPartner`. That client can ask the `ClassMate` object to run its `familyName` script like this:
```java
// cue the study partner
String familyNameOfMyStudyPartner = studyPartner.familyName();
```
The `familyName` script is written to return an object to the one who cued it. Here, the code cueing the script binds that returned object to the identifier `familyNameOfMyStudyPartner`. Because we don't need to send any specific objects to the script (there is nothing inside the "()" part of the invocation) we can alternatively write:
```java
// cue the study partner
String familyNameOfMyStudyPartner = studyPartner.familyName;
```
(You can omit the parentheses only in cases where it doesn't lead to ambiguity, and, unfortunately, that isn't always the case.) We say that the client _sends a cue_ to `studyPartner` requesting that it perform its `familyName` script. This is reminiscent of players in a play saying their piece or performing some action in response to some words or action directed to them from some other actor on the stage. In computerese, what we here call a _cue_ is called a _message_. In this example, the `studyPartner` object (of class `ClassMate`) runs its script — which in this case is very simple. When the script runs it takes the object that was previously associated with its local identifier `familyName_` (identifiers local to a class usually end in an underscore, but that is only a convention) and 'returns' it to the client. The return expression delivers the object to the client in the place where the cue appears in the code. In this example, the identifier `familyNameOfMyStudyPartner` in the client also becomes bound to the same object that `ClassMate` had already bound to its `familyName_` identifier. So `familyNameOfMyStudyPartner` and `familyName_` are now both names for the same object.
Most of the other scripts for `ClassMate` are similar — they serve just to give information to some client. The age script actually needs to do some work — a simple arithmetic calculation. `Date` is another class that knows, obviously, about dates. We can create a `Date` object like this:
```java
Date today = new Date();
```
The text `new Date` is an expression whose result is a new `Date` object. That expression can be used anywhere in the program where a `Date` object is expected. Here, we also declare the identifier today and we associate today with the new object, as a means for asking the object to run its scripts. We can cue the new object to give us the current year:
```java
int thisYear = today.getYear();
```
### 4.5.3 Constructors and Method Parameters
You are probably wondering how and when the identifiers `birthYear_`, `familyName_` and `firstName_` first become associated with an object. They are declared `private` and are inside `ClassMate` so they are inaccessible to the outside world. We need to add a new script to initialize the object, so that these identifiers are all bound to objects. This script is called a _constructor_. We can recognize a constructor because the script name is the same as the class name:
```java
class ClassMate {
public ClassMate() {
familyName_ = "Smith";
firstName_ = "John";
birthYear_ = 2000
}
. . . .
```
Of course, it would be a boring world if all of our classmates where named John Smith. So we allow the client to send information along with their cue to create a new `ClassMate` object — information about the individual for whom the object is being created. Like other scripts, constructor scripts are allowed to have parameters. A declaration of a constructor script with parameters looks like this:
```java
class ClassMate {
public ClassMate(String familyName, String firstName, int birthYear) {
familyName_ = familyName;
firstName_ = firstName;
birthYear_ = birthYear
}
. . . .
```
That is, the method declaration starts with a list of parameter declarations, separated by commas and enclosed in those parentheses you've been wondering about. (The syntax is supposed to be reminiscent of the way mathematicians write functions with their arguments.) We send a cue to create a new `ClassMate` object and to enact this initialization script by saying:
```java
ClassMate someClassMate = new ClassMate("Lastname", "firstname", 1975);
```
Of course, we need only bind those identifiers that we think need to be handled when the `ClassMate` object comes into being; maybe the rest can be bound later by other scripts. However, it is usually good practice to bind all of a class's identifiers to some object inside the constructor.
Putting it all together, we can make a complete running program using the `ClassMate` class:
```java
class ClassMate {
public ClassMate(String familyName, String firstName, int birthYear) {
familyName_ = familyName;
firstName_ = firstName;
birthYear_ = birthYear
}
public String familyName() {
return familyName_
}
public String firstName() {
return firstName_
}
public String nickName() {
return nickName_
}
public int age() {
Date today = new Date();
return today.getYear() - birthYear_
}
private int birthYear_;
private String familyName_, firstName_, nickname_
}
{
ClassMate myStudyPartner = new ClassMate("Funch", "Rune", 1975);
String familyNameOfMyStudyPartner = myStudyPartner.familyName();
System.out.print("Family name of my study partner is ")
.print(familyNameOfMyStudyPartner);
System.out.print(", age of ").println(myStudyPartner.age())
}
```
This is actually a pretty serious program! The last six lines of the program form a block, and a block is just another kind of expression. Since it is the last expression in the program, it is where **trygve** starts enactment. Computer people sometimes call this the 'main program.' It should be very small and tidy — just enough to kick off execution, so the rest of the objects can get about working together to perform the overall pattern of system enactment.
This program also does a bit of showing off with respect to how we can chain together `print` and `println` requests. `System.out` is just a special object of class `OutputStream` ("special" in the sense that, at this writing, you cannot create your own objects of this class). The scripts `OutputStream.print` and `OutputStream.println` both return the output stream for which they were called. So `System.out.print(123)` is an expression that evaluates to `System.out`. That means we can say `(System.out.print(123)).print(456)` to print 123456. Or we can omit the parenthesis and just write `System.out.print(123).print(456)` to save some typing.
### 4.5.4 Scripts at several levels
You'll notice that the above program has several scripts that we can see inside class `ClassMate`, as well as the final script that starts things off. These are the two most common kinds of **trygve** scripts. Most **trygve** scripts live inside a class and become available to users of objects of that class.
We'll later learn about other concepts which, like classes, group scripts together, sometimes with data and sometimes not. These concepts are:
- The Context, which represents a graph of interactions between objects
- Roles, which contain scripts for archetypes that we carry around in our head
- Interfaces, which are just a way of grouping classes with similar behaviour
### 4.5.5 Identifiers Inside a Script
A script can declare its own identifiers for its own use, inaccessible to any other code. The script can associate these identifiers with the objects passed as parameters, with objects that are returned when cueing other scripts, or with objects that the script creates.
We might create a script to evaluate one of the roots of a simple polynomial. To make the code easier to understand we can break the evaluation into parts. We can declare an identifier for each one of the sub-expressions and combine them into an answer:
```java
double polyRoot(double a, double b, double c) {
double four_a_c = 4.0 * a * c;
double denominator = a + a;
double rootArg = b ** 2 – four_a_c;
double root = (-b + Math.sqrt(rootArg)) / denominator;
return root
}
```
As we showed above, we can put several scripts inside of a class. We can break down the polynomial evaluation into small scripts, each one of which contributes its part to the answer:
```java
class Polynomial {
public Polynomial(double a, double b, double c) {
a_ = a; b_ = b; c_ = c
}
private double four_a_c() {
return 4.0 * a_ * c_;
}
private double denominator() {
return a_ + a_;
}
private double rootArg() {
return b_ ** 2 – this.four_a_c();
}
public double root() {
return (-b_ + Math.sqrt(this.rootArg())) / this.denominator();
}
private double a_, b_, c_;
}
```
First, you'll notice that many of the scripts are declared as `private` — they are private to class Polynomial. That is because they are of no interest or use outside the class. Only other scripts within Polynomial may cue a request to these scripts.
Second, you'll notice that the script called `root` cues two other scripts: `rootArg` and `denominator`. It does so by sending a cue within the same object, which we designate with the identifier `this`. The identifier this is implicitly declared for you by **trygve** within every script, and it always refers to the object containing the script.
## 4.6 Flow Expressions
A script may need to improvise its behavior when it is enacted, perhaps because it has been designed to handle multiple related situations and it has to wait until enactment to know which one applies. This means that we need to be able to steer the flow of expressions during enactment. Or maybe there is some sequence of expressions that we want to do evaluate several times: we want to be able to write it just once but enact it many times, like the chorus of a song.
### 4.6.1 The IF expression
The IF expression lets you ask a yes/no question derived from some state of an object and then to evaluate either one expression or another depending on the answer. So you might write code for a car in China that looks at the license plate number: its oddness `(licenseNumber % 2 == 1)` and the evenness or oddness of the date `(Date.getDate() % 2 == 1)` and to allow the driver to drive only if the evenness of both the date and the license number are the same:
```java
boolean canDrive = if (licenseNumber % 2 == 1)
Date.getDate() % 2 == 1
else
Date.getDate() % 2 == 0
```
(Of course, logicians could state this same condition in a much more compact way, but this way is easy to read and helps illustrate what we're trying to demonstrate here.) The IF expression is called a _conditional expression_. It is one of the key building blocks of business logic. Its use also arises in mathematics. Here's an elegant formulation of factorial in **trygve**:
```java
class Fact {
public int fact(int n) {
return if (n <= 1) 1 else n * fact(n-1)
}
}
```
Let's go back to our polynomial roots example. We've already seen the code:
```java
public double root() {
return (-b_ + Math.sqrt(this.rootArg())) / this.denominator();
}
```
This works as long as `this.rootArg()` is non-negative. If it is negative, then the polynomial has a complex root with an imaginary component.
Instead of writing a script that returns the root to another script in response to a cue, we might instead write a method that just prints both of the roots of the polynomial — including any complex ones.
Let's say that the square root argument is negative: that would cause trygve to encounter the uncomfortable position of trying to take the square root of a negative number, and we want to save it that embarrassment. We'll assume for the time being that we aren't interested in imaginary or complex solutions. We can write:
```java
public void printRoot() {
double rootArgument = this.rootArg();
if (rootArgument > 0) {
System.out.print("root is ")
.println(-b_ +
Math.sqrt(rootArgument) / this.denominator())
} else {
System.out.println("root is complex")
}
}
```
#### Side-Effects
Like all expressions the `if` expression yields a result. Like most other expressions, it's a fusion of other expressions working together. We can divide **trygve** expressions into two kinds: those that have _side-effects_, and those that don't. A side-effect is usually a change in some state that persists after the expression has completed its enactment. If one actor in a play just speaks a part, we can't tell by looking at the scene whether that player has spoken that line or not: there is no residual side-effect. But if one player spits at another we can deduce even after-the-fact (at least for a short while) that we had come to that point in the script. The player left a side-effect.
The IF expression is often used for its side-effect alone in many programming languages, and it can be used this way in **trygve**. Let's return to our polynomial example, and revert the printRoot example back to the original version that returned a result.
### 4.6.2 FOR loops and vectors
Sometimes we want to group many items of the same kind together, like a flock of geese or a collection of test scores or a list of attendees at a party. And we may want to enact some script for each of them in turn: e.g., to print an invitation for each of the people we want to invite to the party. If we know the number of items we'll have to deal with, we can use a vector. We can create a vector of `int`s or of `String`s or of `ClassMate`s of a given size.
We must declare a vector before we can use it. A vector of `int`s to hold all the scores from a class test might be declared like this:
```java
int numberOfClassMembers = 23;
int [] scores = new int[numberOfClassMembers]
```
The above vector is called `scores` and has 23 _elements_. Before we arrange otherwise, each element of the vector is associated to the `null` value. Each element of the vector is just a name, and we can associate each of those elements with an object, like this:
```java
int joesScore = 96;
scores[5] = joesScore
```
These expressions first cause `joesScore` to become a name for the `int` object whose value is 96, and then cause `scores[5]`to become another name for that same object. If we had instead said:
```java
int joesScore = 96
int joesIndex = 5
scores[joesIndex] = joesScore
```
the result would be exactly the same: both `scores[5]`and `joesScore`would become names for the same object carrying Joe's score.
Alternatively, we can say
```java
scores.atPut(joesIndex, joesScore)
```
to replace the element at `joesIndex` with the object `joesScore`, and can use
```java
scores.at(joesIndex)
```
as an alternative to `scores[joesIndex]`. This will become useful when we specify a kind of contract for a Role, when we are expecting a vector as a Role-player. Most of the time you'll use the more straightforward, algebraic-like syntax.
We normally "fill up" an entire vector with values before it is used; however, there is nothing in **trygve** that says you must do this. If you do fill up the entire vector, then you can very expressively process each of its elements, one at a time. We do this with another expression called a _for expression_. It looks like this:
```java
for (int aScore : scores) total = total + aScore
```
So, for example, we could compute the average score like this:
```java
int scoreCount = 0
double total = 0
for (int aScore : scores) {
total = total + aScore
scoreCount++
}
double averageScore = total / scoreCount
```
The expression `scoreCount++` simply increments the value of `scoreCount` by 1. You notice that we use a `double`instead of an `int` to total the scores. If we had summed them as an integer and then divided by an integer, **trygve** would give us an integer result: we would lose the precision in the fractional part. This follows a common convention used in Java and in most modern programming languages.
There is an alternative form of the `for` expression that isn't limited to working with collections like vectors. It is a general way of running an index over a predefined range with a regular increment, and it looks like this:
```java
for (int i = 0; i <= 22; i++) {
System.out.print(i)
}
```
This loop will print the integers from 0 to 22 inclusive. It works like this: The `for` statement in this form ties together four expressions:
```
for (initialization expression; test expression; increment expression) body expression
```
The `for` loop starts by evaluating the _initialization expression_. It can be any expression or, as indicated in the above example, it may alternatively even be a declaration. By tradition and convention it declares and initializes a simple integer, often called the _loop variable_. The `for` expression will cause the loop variable to successively take on increasing values so that it can be used, for example, as an index into a vector.
Then the _test expression_ is evaluated. It must evaluate to a boolean result. If the result is false, then the `for` expression is done and enactment will pick up at the next part of the script in the flow. Otherwise, the _body expression_ is evaluated. The body expression is most often a _block_: a sequence of expressions grouped together by surrounding curly braces so that, from the outside, they are all treated as one large expression. Once the _body expression_ has been enacted, the `for` expression evaluates the _increment expression_. Again, this can be any expression, but by tradition and convention it increments the loop variable. Last, we return again to the _test expression_, and the loop continues again through the _body expression_ and _increment expression_ and back again, until the _test expression_ causes the looping to come to an end.
So revisiting the loop from above,
```java
int scoreCount = 0
double total = 0
for (int aScore : scores) {
total = total + aScore
scoreCount++
}
double averageScore = total / scoreCount
```
we could just have well have written it like this:
```java
int scoreCount = scores.size()
double total = 0
for (int i = 0; i < scoreCount; i++) {
total = total + scores[i]
}
double averageScore = total / scoreCount
```
#### A Puzzle
It's perhaps time for a little quiz! Given what you now know about objects and vectors, what is the output of the following program?
```java
class VectorTest {
public void test() {
int [] intVector = new int[5];
for (int i = 0; i < 5; i++) {
intVector[i] = i
}
for (int i = 0; i < 5; i++) {
System.out.println(intVector[i])
}
}
}
(new VectorTest()).test()
```
### 4.6.3 While loops
The `for` loop is just one kind of loop in **trygve**, and there are two others: `while` loops, and `do`/`while` loops. These kinds of loops are all basically the same in how they structure computation; the difference is in the boundary conditions, in relative complexity, and in whether they check for completion at the beginning or the end of the iteration.
The `while` loop is perhaps the simplest kind of loop in **trygve**. Generically, it is of the form:
```
while (condition expression) body expression
```
It works by first evaluating the _condition expression_, which can be any expression that evaluates to a `boolean`. If it evaluates to `false` then enactment continues at whatever expression follows the _body expression_. Otherwise, the _body expression_ is executed once. Then we just start over again, and go back to evaluate the _condition expression_, and then the _body expression_ again if the result evaluates to `true`, and so forth. The iteration stops when then _condition expression_ finally evaluates to `false`.
Note that this implies that the _body expression_ has some side effect on which the _condition expression_ depends; otherwise, you will loop forever!
The `while` loop can be used with any expressions that require doing something again and again. Whereas the `for` loop is usually used when the number of iterations is known ahead of time, the `while` loop is typically used when we can know only just-in-time when we are done.
Consider implementing Newton's Method, which takes an unpredictable number of iterations to find a root (adapted from: http://www.cplusplus.com/forum/general/94191/):
```java
double root(double number, double lower, double upper, double guess) {
double ACCURACY=0.001;
if (number < 1) {
lower = number;
upper = 1.0
} else {
lower = 1.0;
upper = number
}
while ((upper-lower) > ACCURACY) {
guess = (lower + upper) / 2.0;
if (guess*guess > number) upper = guess
else lower = guess;
}
return (lower + upper) / 2.0;
}
```
### 4.6.4 Do / While loops
A `do` / `while` loop is like the `while` loop above except that the test expression comes at the end:
```java
do body expression while (condition expression)
```
In a `do` / `while` loop the _body_ _expression_ is always executed at least once. We can use the worst sorting algorithm ever, Bubble Sort, to illustrate (courtesy of [http://www.algolist.net/Algorithms/Sorting/Bubble_sort](http://www.algolist.net/Algorithms/Sorting/Bubble_sort), slightly modified):
```java
public void bubbleSort(int [] array) {
boolean swapped = true;
int j = 0;
int tmp;
do {
swapped = false;
j++;
for (int i = 0; i < array.size() - j; i++) {
if (array[i] > array[i + 1]) {
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true
}
}
} while (swapped)
}
```
### 4.6.5 Exiting a Loop
The **trygve** language lets you jump out of a loop prematurely if you should find the need. The `break` statement causes any looping construct to stop enactment of the loop immediately. Enactment ensues immediately following the loop.
Use `break` statements sparingly. They interrupt the normal flow of execution and can make a program slightly more difficult to comprehend than if they weren't there.
### 4.6.6 Switch Expressions
A `switch` expression is kind of a poor man's table lookup. Here is an example from a program by Andreas Söderlund that uses a switch statement to map a chord name to a position:
```java
public int position() const {
int pos =
switch (name()) {
case "C": 1; break;
case "C#": 2; break;
case "Db": 2; break;
case "D": 3; break;
case "D#": 4; break;
case "Eb": 4; break;
case "E": 5; break;
case "F": 6; break;
case "F#": 7; break;
case "Gb": 7; break;
case "G": 8; break;
case "G#": 9; break;
case "Ab": 9; break;
case "A": 10; break;
case "A#": 11; break;
case "Bb": 11; break;
case "B": 12; break;
default: 0; break
}
}
```
In general the `switch` statement is of the form:
```
switch(switch-expression) {
case constant: expression
case constant: expression
default: expression
}
```
The statement first evaluates the _switch-expression_ to get an object. It then checks each of the _case labels_ (those parts of the above description labeled as _constant_) to look for an exact match. (These `case` labels are just tags in the code: they aren't really "enacted" per se, but just anchor some points at which enactment might ensue, depending on the `switch` value). When it finds a match, it continues enactment with the corresponding _expression_.
Case labels are transparent with respect to enactment: that is, enactment will proceed right through them without doing anything. So to keep the above example from executing every expression we add a `break` expression at the end of each one. This causes enactment to jump out of the `switch` statement and to continue at whatever follows. That means that the above `switch` statement can be made a little less redundant by rewriting it as follows:
```java
public int position() const {
int pos =
switch (name()) {
case "C": 1; break;
case "C#":
case "Db": 2; break;
case "D": 3; break;
case "D#":
case "Eb": 4; break;
case "E": 5; break;
case "F": 6; break;
case "F#":
case "Gb": 7; break;
case "G": 8; break;
case "G#":
case "Ab": 9; break;
case "A": 10; break;
case "A#":
case "Bb": 11; break;
case "B": 12; break;
default: 0; break
}
}
```
Of course, you can use a `switch` expression just for its side effects instead of to generate a value. Again, here we unnecessarily introduce a variable that represents state, when, in fact, this is a table lookup that has no state:
```java
int weekdayIndex(String weekdayName) {
int retval;
switch (weekdayName) {
case "mandag": retval = 1; break;
case "tirsday": retval = 2; break;
case "onsdag": retval = 3; break;
case "torsdag": retval = 4; break;
case "fredag": retval = 5; break;
case "lordag": retval = 6; break;
case "sonday": retval = 7
}
return retval;
}
```
The expression `weekdayName` is evaluated and then enactment ensues at the expression matching the `case` label associated with the corresponding value. If none of the values match the expression, you can include a `default` label to catch outliers. If there is no `default` label then the `switch` statement effectively does nothing if no cases match the expression. So in this function, weekends would be caught by the default clause:
```java
int workingDayIndex(String weekdayName) {
int retval;
switch (weekdayName) {
case "mandag": retval = 1; break;
case "tirsday": retval = 2; break;
case "onsdag": retval = 3; break;
case "torsdag": retval = 4; break;
case "fredag": retval = 5; break;
default: System.out.print(weekdayName).println(" is not a working day")
}
return retval;
}
```
It's always a good idea to include a `default` case! And unless you want to use the `switch` statement just for its side effects, all of the case expressions have to be of the same type (which means that the last expression in each expression list after the `case` label, is of the same type.)
Note that the `switch` statement works a bit different than in most C-based languages. In C, the `case` expressions may be in any enclosed scope of the `switch`. In **trygve** they must be in the scope immediately enclosed by the `switch` expression. Also, C limits the `case` types to `int`s; **trygve** supports a much broader set of types, including `String`s.
# 5. Class-Oriented Programming
The most popular programming languages today include Java, C++ and C#. While most people call most of these object-oriented languages, in fact, one writes objects in none of them. Most of the focus of these languages is on _classes_. (One can view Javascript as a more true object-oriented language, but we won't discuss that in detail here.) Classes go back at least to the Simula 67 programming language which many herald as the first, er, object-oriented programming language.
Classes are an extremely important construct whose main value is to capture the primary structure of a program. In most programming languages, classes are the factories or moulds from which objects are built, and the structure of any given object can be traced to its class. To a first approximation this is true in **trygve**. Classes represent our conceptualization of what the system _is_: they are the building blocks of modern programs. In most languages, they exist in the source code; i.e., classes can exist before you have a running program. In many programming languages (like **trygve**, but unlike C++) these classes live on into run-time.
Classes incorporate many of the building blocks of previous-generation programming languages, combining procedural scripts with declarations into one tidy package where the parts fit well together and build on each other to offer more complex chunks of reasoning than procedures alone can. So if we need a stack, we can actually create a single entity that quacks like a stack, walks like a stack, and flies like a stack. We implement that entity as a class, where the class is the assembly of all of the scripts and declarations we would otherwise use to implement stack functionality in an old language like FORTRAN or Pascal, or in C or some other assembly language.
Classes have a close relationship to another important programming building block called the _abstract data type_, exemplified in a language called Clu. Much of the focus of early C++ work was to provide abstract data types (ADTs) to C programmers. The "abstract" in _abstract data type_ refers to the usual way of articulating them as just a collection of APIs that distanced the client from having to worry about how things were implemented internally. These APIs are like contracts for users of an object.
In some sense ADTs exist only in our mind (or they may show up in system architecture documentation where we want to conceptualize the big pieces). However, like all languages, computing languages should be good at communicating the stuff we care about and work with, so it would be good to be able to actually _write_ an ADT. And in **trygve** we can: the artefact is called an _interface_. **An _interface_ is a contract by which some client uses an object**.
There has been much confusion around this notion over the years. It's important to keep thinking of an abstract data type as something that's kind of abstract: about whose internals we do not have to bother. Of course, there must be something concrete behind that abstraction if we are to execute real programs! Sometimes ideal computer scientists get lost in the abstraction and forget about the need for concrete stuff. And that's what classes are. **Classes are the implementations of abstract data types**.
Let's start concretely and then climb up the abstraction ladder.
## 5.1 A concrete, complex class
Those of you who have started to take algebra know about complex numbers. We can think of a complex number as a number with two parts: a _real part_ and an _imaginary part_. Engineers and other folks in the physical sciences use them to model many physical phenomena. If you don't have experience with complex numbers, for the time being just take our word for it that they're useful and that each one is really two numbers rolled into one.
We can write a class for a complex number like this:
```java
class Complex {
public Complex(double realPart, double imaginaryPart) {
this.realPart_ = realPart;
this.imaginaryPart_ = imaginaryPart
}
public Complex(double realPart) {
realPart_ = realPart; imaginaryPart_ = 0.0
}
public Complex() { realPart_ = imaginaryPart_ = 0.0 }
public Complex +(Complex other) {
double resultingRealPart = other.realPart() + realPart_;
double resultingImaginaryPart = other.imaginaryPart() +
imaginaryPart_;
Complex retval =
new Complex(resultingRealPart, resultingImaginaryPart);
return retval
}
. . . .
public String toString() const { . . . . }
public double realPart() const { return realPart_ }
public double imaginaryPart() const { return imaginaryPart_ }
private double realPart_, imaginaryPart_;
}
```
Most (but not all) class names start with an upper case letter. (The **trygve** language currently does not enforce this but it's good to be attentive to this very common convention.)
### 5.1.1 Classes and Constructors
Let's take apart this class one piece at a time. At the outermost level is a wrapper:
```java
class Complex {
. . . .
}
```
These lines wrap a declaration, where the declaration, well, _declares_ the class Complex. We fill in the declaration with our instructions to the **trygve** language about what this class should do. The first script is a special script called a _constructor_, and it is used to initialize objects of class Complex:
```java
public Complex(double realPart, double imaginaryPart) {
this.realPart_ = realPart;
this.imaginaryPart_ = imaginaryPart
}
```
The **trygve** environment calls this constructor on our behalf when we create new objects of class Complex. You notice that the constructor has two parameters, `realPart` and `imaginaryPart`. We need to supply these parameters when we create the object. We do that like this:
```java
Complex myComplexNumber = new Complex(1.0, -1.0)
```
This will create a new `Complex` object with a real part of 1.0 and an imaginary part of –1.0. When we make this enactment request, `realPart` becomes a name for the object representing 1.0 and `imaginaryPart` becomes a name for the object representing -1.0. It remembers these numbers by associating them with identifiers inside `Complex` itself, and we find their declaration at the very bottom of the class:
```java
private double realPart_, imaginaryPart_;
```
You may notice that the declaration is prefixed with the word `private`. This is called an _access modifier_. It controls what scripts can interact with `realPart_`and`imaginaryPart_`. By making these two identifiers `private`we're telling **trygve** that their objects should be accessible only to scripts inside class Complex itself. By the same token you notice that most of the scripts are declared as `public`, which means that the scripts can be enacted by any stakeholder that is using the Complex class.
Back to the constructor — we stored away these parameters by saying:
```java
this.realPart_ = realPart;
this.imaginaryPart_ = imaginaryPart
```
What this means is: "take the object named by `realPart` and also bind it to the `realPart_` identifier that is inside the current object, `this`", and then to do the analogous thing for the `imaginaryPart`. The identifier `this` is always bound to the object to which the current script belongs. When referring to members of the current object we can usually omit it; that is, the above lines could just as well have been written:
```java
realPart_ = realPart;
imaginaryPart_ = imaginaryPart
```
You'll also notice that these identifiers end with an underscore ("_") character. This is a convention used when naming object declarations inside a class; again, it is up to you, and not up to the compiler, whether you want to do this.
Next, we find this script:
```java
public Complex(double realPart) {
realPart_ = realPart; imaginaryPart_ = 0.0
}
```
It looks a little like the other one but takes only one parameter, which is the real part of the complex number. It is another constructor. This is used if we want to create a complex number whose imaginary part is zero. We can say
```java
Complex myComplexNumber = new Complex(2.0)
```
Of course, this is identical to writing:
```java
Complex myComplexNumber = new Complex(2.0, 0.0)
```
and either is acceptable. And there is a third constructor:
```java
public Complex() { realPart_ = imaginaryPart_ = 0.0 }
```
that just initializes the new complex object with both its real and imaginary parts zeroed out.
Notice that we took great care to initialize the realPart_ and imaginaryPart members of the object inside each of the constructors.
```java
. . . .
public Complex(double realPart) {
realPart_ = realPart; imaginaryPart_ = 0.0
}
. . . .
private double realPart_, imaginaryPart_;
```
As a syntactic convenience we can initialize these members in place instead:
```java
. . . .
public Complex(double realPart) {
realPart_ = realPart;
}
. . . .
private double **realPart_ = 0.0**, **imaginaryPart_ = 0.0**;
```
The initializations actually take place when any constructor is enacted to bring a new object into existence.
### 5.1.2 Teaching the Compiler about New Types
The next script is a bit more interesting:
```java
public Complex +(Complex other) {
double resultingRealPart = other.realPart() + realPart_;
double resultingImaginaryPart = other.imaginaryPart() + imaginaryPart_;
Complex retval = new Complex(resultingRealPart, resultingImaginaryPart);
return retval
}
```
This script defines what **trygve** should do if we ask a `Complex`object to enact the "+" script. That looks like this:
```java
Complex c1 = new Complex(1.0), c2 = new Complex(2.0);
Complex c3 = c1.+(c2)
```
That would cause the `realPart_` identifier in `c3` (a new object, different from either `c1` or `c2`) to be bound to a `double` object with the value 3.0. Of course, this syntax looks pretty strange and computer-like: we're explicitly calling a script named "+", using the usual notation with a period that we use when enacting other scripts. While this syntax works, **trygve** recongises this equivalent way of saying the same thing:
```java
Complex c1 = new Complex(1.0), c2 = new Complex(2.0);
Complex c3 = c1 + c2
```
which is just how you learned it in algebra class.
There is something fundamental going on here, and it's worth making it explicit. When we're writing code like this, we're teaching the compiler how to recognize new types. The **trygve** environment already recognizes a "+" operator (as well as "-", "*", "/", and a few others) on `int` and `double` types. The compiler-writers baked that into the compiler when they wrote it. Now, **trygve** is giving you a chance to effectively extend the definition of the language. You are teaching the compiler how to work with a new type: `Complex`. It's a type you created. Of course, all the scripts of the Complex class do this in varying degree, but it's all the more obvious here with the ability to redefine the semantics of an ordinary operator. You can't do this with all operators.
You can also change the way that the relational operators (like "<" and "==") work. These operators are special in that there is some redundancy across them. That is, if you know "<" and "==" you can write the code for ">" in terms of those two (if neither "<" nor "==" is true then ">" must be true) and you of course can trivially write the code for "<=". It can be tedious and error-prone to make all of these work consistently — and the consequences of getting it wrong can be very confusing or even disastrous.
So **trygve** takes a simpler approach, inspired by Java. If you have a class for whose objects you want to support these operators, you just give That Class the following script:
```java
public int compareTo(ThatClass other) { . . . . }
```
It is a very simple function: it must return an `int` object and it must take one parameter, which is usually of the same type as the class in which we install it as a script. If `a` is less than `b` and we call a.compareTo(b), it should return -1. If `a` equals `b` then it should return 0. And if `a` is greater than `b` then it should return 1. The **trygve** environment will take care of the rest, and will arrange to call `compareTo` on your behalf when you use any of the built-in relational operators. You can of course enact the `compareTo` script directly. But it is not allowed to define a method whose name is any of the relational operators. We teach the compiler about comparison with the addition of this one simple function: the compiler will guarantee consistency across all the relational operators.
### 5.1.3 Accessors: Everyday scripts
Last, we find some pedestrian scripts that just answer our questions about the values of the real and imaginary parts of the object, or which render the object as a `String`:
```java
public String toString() const { . . . . }
public double realPart() const { return realPart_ }
public double imaginaryPart() const { return imaginaryPart_ }
```
There is nothing mysterious or special about these, except they allow us to access the information hidden away in the objects attached to the `private` identifiers. They are sometimes called _accessors_. You notice that the modifier "const" follows the first part of the declaration. This tells **trygve** that these scripts promise not to modify any of the object state when they execute. Any script may be made `const` but **trygve** will enforce it. You should strive to make as many of your scripts `const` as possible.
You should also make it a habit to include a `toString` script for most classes you write. It comes in handy when you are displaying them for human consumption. Most built-in types have a `toString` script built-in.
## 5.2 An Alternative Implementation of Complex, and Interfaces
Here is another class:
```java
class Complex {
public Complex(double realPart, double imaginaryPart) {
theta_ = Math.asin(imaginaryPart / realPart);
r_ = Math.sqrt(realPart ** 2 + imaginaryPart ** 2)
}
public Complex(double realPart) {
r_ = realPart; theta_ = 0.0
}
public Complex() { r_ = theta_ = 0.0 }
public Complex +(Complex other) {
double resultingRealPart = other.realPart() + realPart_;
double resultingImaginaryPart = other.imaginaryPart() + imaginaryPart_;
Complex retval = new Complex(resultingRealPart, resultingImaginaryPart);
return retval
}
. . . .
public String toString() const { . . . . }
public double realPart() const { return r_ * Math.asin(theta_) }
public double imaginaryPart() const { return r_ * Math.acos(theta_) }
private double r_, theta_;
}
```
It also implements complex numbers! The first one is often called "Cartesian complex numbers" and this one "polar complex numbers"? Would we ever want to keep both of them around in our library of classes? Maybe. Maybe one or the other would be particularly suitable to some set of algorithms because its approach would prove more efficient than the other. Yet they both faithfully represent complex numbers. From a logical point of view they are completely interchangeable. We say that they are alternative implementations of the type Complex. Remember: a class is an _implementation_ of a type.
Let's say that someone wanted to write some generic code that abstracted away the difference between these two implementations. We make that code dependent on the common interface to these two classes. To that end **trygve** offers a construct called an _interface_, which is a kind of declaration. It looks the `public` bits of the class with all the implementation parts thrown away:
```java
interface Complex {
public Complex(double realPart, double imaginaryPart);
public Complex(double realPart);
public Complex();
public Complex +(Complex other);
. . . .
public double realPart();
public double imaginaryPart() ;
}
```
The interface will bear the name Complex: it represents the essence of the concept. It is a pure abstract data type. It compiles, but it's really empty — there are no real scripts or declarations here. It is a kind of a contracts that shapes how the implementations must be written.
Given that the name Complex is now taken, we have to rename our two previous attempts. (We were already in trouble because we can't have two classes with the same name at the same level in the same system.) Let's call them `CartesianComplex` and `PolarComplex`. Each one will be rewritten as a new declaration with an explicit allegiance to `Complex`:
```java
class CartesianComplex implements Complex {
public CartesianComplex(double realPart, double imaginaryPart) {
realPart_ = realPart; imaginaryPart_ = imaginaryPart
}
. . . .
}
class PolarComplex implements Complex {
public PolarComplex(double realPart, double imaginaryPart) {
theta_ = Math.asin(imaginaryPart / realPart);
r_ = Math.sqrt(realPart ** 2 + imaginaryPart ** 2)
}
. . . .
}
```
Somewhere in our code we'll find a client of Complex numbers — in some script of some class. Maybe that method receives a Complex number from one of _its_ clients . We might write the script like this:
```java
class SomeClass {
public Complex someScript(Complex oneNumber, Complex anotherNumber) {
Complex retval = oneNumber * anotherNumber + 3.13 * oneNumber;
return retval;
}
}
```
The client of this class can choose to work with it in terms of a `CartesianComplex` or a `PolarComplex`, or both, at its whim:
```java
CartesianComplex one = new CartesianComplex(1.0), two = new PolarComplex(2.0)
SomeClass someObject = new SomeClass();
someObject.someScript(one, two)
```
Alternatively, we could have written:
```java
Complex one = new CartesianComplex(1.0), two = new PolarComplex(2.0)
SomeClass someObject = new SomeClass();
someObject.someScript(one, two)
```
So we see that `Complex` works as a generic type. Classes can be used to create types that behave just like the built-in types `int` and `String`, and we can use a class name just about everywhere we can use the name of a "regular" type like `String`. Interfaces can be used to group together alternative class implementations that may be used interchangeably. We can also use interface names just about everywhere that an ordinary type name can appear, with one exception: we can't of course say new InterfaceTypeName.
If you declare that a class implements an `interface`, the **trygve** environment will ensure that it has the right to do so. That is, every script published in the `interface` must also be implemented by the `class`. And only script declarations may appear in the interface — no object declarations!
It's not common, but a class may implement multiple simultaneous interfaces:
```java
class PolarComplex implements Complex, ConvertibleToString {
. . . .
}
```
This gives the class the ability not only to be used anywhere a `Complex` is expected, but also anywhere any other `ConvertibleToString`is expected. We might have another class which is not a `Complex` but which is a `ConvertibleToString`:
```java
class ClassMate implements ConvertibleToString {
. . . .
public String toString() {
String retval = this.firstName() + " /"" +
this.nickName() + "/" " +
this.familyName();
return retval
}
}
```
So if we had a `ClassMate` object in hand, and if we add this `toString` script, we could ask the object to run that script to give us a `String` representation of the classmate's name. Maybe in some application we want to keep all of our complex classmates in a vector together with a bunch of `Complex` numbers, to collect all of our "complex" things together, so to speak:
```java
ConvertibleToString [] complexThings = new ConvertibleToString [2];
complexThings[0] = new ClassMate("Smith", "John", 1969);
complexThings[1] = new CartesianComplex(5.0);
```
We could then print out the entire vector, which is now a mixed collection of objects of different classes — but all of _type_ `ConvertibleToString`:
```java
for (String printable : complexThings) System.out.println(printable)
```
### 5.2.1 What is abstraction?
By the way, we used two sophisticated words in the last section — _generic_ and _abstraction_. These two words are often misunderstood.
Abstraction means to simplify by throwing information away. Throwing information away is always dangerous because, most of the time, we need every bit of information we can get! But sometimes too much information is just too much. If you're a manager and you want to know about your employees' progress on some project you probably should be getting regular updates on the coarse, big picture. To look at every facet of every everyday task is called micromanagement. So good managers abstract away the details. To do that takes trust.
If one piece of software trusts another piece of software, it can make the interaction easier to manage by using abstraction. We demarcate a boundary between the two pieces of software (e.g., between a client method and the class of the object from which it is receiving service) with an abstraction boundary. A good class interface is an abstraction boundary, and that boundary is punctured only at the points of `public` declarations. Because it is devoid of any implementation, an interface is an almost perfect abstraction boundary.
If we don't have to care about the details of how classes do their work, then the interface can represent a generic interface to one or more implementations. It not only can better hide the implementation of a single class but can make the differences between several alternative classes transparent. We still get to choose between the implementations (e.g., for engineering reasons) but that decision can be decoupled from the way our code interacts with the "provider of choice." It's like buying generic soap: we care what it does without caring too much about how it was made or who the provider is: yet at some point we make a decision to buy a certain kind of soap, and we are free to change our decision next time. This balance between complete interchangeability, and the techniques that make substitutability transparent, is maybe the central theme of class-oriented and object-oriented programming.
### 5.2.2 Interface Guidelines
Classes, objects, interfaces — so many concepts! There will be more. And we'll return to this topic later to clarify which tool to use when. But it's important at this point to say something about interfaces. Interfaces live in the world of classes and of the major system building blocks. They do not live in the arena of the system's functionality, of its use cases. We have another language facility to cover that: Roles.
So good interfaces organize together classes without regard to the appearance of their objects in use cases. It is a "purely structural" classification. Taking this common engineering rule to heart will make it easier for us all to understand each others' code, and increases the amount of information conveyed by an interface declaration — because we can assume that the designer is trying to use it to organize the classes, rather than to unify any use case logic.
## 5.3 Generic Classes
One of the uglier but most celebrated parts of C++ is its template system. What started as a text-substitution facility to stamp out cookie-cutter clones of a common glob of code eventually made it into the compiler as a language feature. Java adopted this feature and, first, sadly called it "generics," though it is just one form of generic typing, and second, implemented using an approach that suffers from "type erasure" that takes away most of the clever text-substitution power. It has nonetheless become a staple of how Java programmers deal with collections, and we include it in **trygve** for the sake of familiarity. In some sense, it is a poor man's substitute for manually generated broad, shallow inheritance hierarchies.
Generic classes are related to the above discussion of interface. We can use an interface to group together multiple classes whose implementations are wildly different. The interface declaration stipulates the rallying point of what those classes must have in common.
Generic classes are much the same except that the source code for all of the classes is exactly the same. How they differ is in the types of objects they interact with. There are always business needs that arise where we want to capitalize on very low-level commonality between classes, but in a way that would be inconvenient for the programmer to highlight in the code. For example, the `List` generic may care only that its objects all have a `compareTo` operation, yet there may be no guarantee that all such classes adhere to a single industry-standard interface called `Comparable` that ties them together. (Such an interface doesn't historically exist at all in C++ and though the Java library provides such an interface, there is no guarantee that all classes with comparable behaviour pay it homage). So generic classes were created. They allow the compiler to actually analyze the code of the classes that the generic is interacting with and to bind the generic to its client objects at the points where scripts like `compareTo` exist. This made it possible for a single generic class to use classes from several different, uncooperative sources according to their adherence to common conventions, in the absence of the engineering discipline to create type groupings at an industry level or even a project level. Without that discipline it is impossible to leverage interfaces, but generic classes come to the rescue for low-level concerns.
That said, you _can_ use generic classes to express high-level groupings among the classes of a cooperative bunch of good people. However, in that case it's almost always better to use interfaces instead.
So, down to nuts and bolts. The **trygve** environment comes with some built-in generic types, one of which is `List`. One declares and creates `List`s like this:
```java
List intList = new List()
```
Read as a single concept, `List`is a class. As a class it behaves like any other class. There may be another class `List`. However, these two classes have no more to do with each other than class `Apple` and class `Orange`. But the phrase List may be used anywhere a class name may be used.
Though they can't interact in any powerful way at run time, all List classes offer exactly the same set of facilities to the programmer. I can add items to a List:
```java
intList.add(1); intList.add(2)
```
or I can ask its size:
```java
int size = intList.size()
```
or ask for the index of where a given item appears in the List:
```java
int theIndex = intList.indexOf(2)
```
A List is very much like a vector, except it grows at run-time according to need. You can find a more complete description of List and other generic collections in the index. What's important to note here that, as a generic class, List can broaden to handle a wide range of types. So by analogy to the above, we can also write:
```java
List stringList = new List();
stringList.add("one"); stringList.add("two");
int size = intList.size();
int theIndex = intList.indexOf("two")
```
Though this has nothing to do with generics, remember: When using collections like `List`, `Set` and `Map`, that if you are putting any objects of your _own_ classes in them, that your classes should define the `compareTo` operator as a public script. Your `compareTo` will be consulted by scripts such as the `contains` script of `List`, which will compare its argument with every element of the `List` in turn until it finds a match (i.e., your script `compareTo` returns zero, which means value equality. An integer less than zero means that "<" is true, and an integer greater than zero means ">" is true — if they apply). If you do not provide a `compareTo`script then `List`, `Set` and `Map` will use identity comparison for scripts like `contains`, `remove`,`indexOf`, and others (in other words, these scripts will find a match only if it is exactly the same object).
### 5.3.1 Defining generics
**NOTE: We strongly discourage you from creating your own generics.**
You define a generic class like this:
```java
class MyGeneric {
. . . .
}
```
The parameters are placeholders for type names that the client will provide. These parameter names can be used within the generic declaration just as other type names may be used:
```java
class MyGeneric {
Parameter1 myFunction(Parameter2 foo) {
Parameter1 retval = new Parameter1(foo.aNumber());
return retval
}
}
```
Try doing that in Java.
## 5.4 Inheritance
Inheritance is a relationship between classes. In short, we start with a _base class_ that approximates the result that we want. We create another class called a _derived class_ that describes a delta of additions to the base class to get us where we are going, and we use inheritance to put them together to create a new class (named after the second class) that incorporates an increment of functionality over the original. The syntax looks like this:
```java
class DerivedClass extends BaseClass {
// the definition of DerivedClass declarations and scripts
}
```
One original purpose of inheritance was to realize code reuse: the derived class "reuses" the code of the base class. However, experience over the years has dampened enthusiasm about inheritance as a reuse feature. First, it's not clear that it's any more powerful as a code reuse mechanism than one procedure reusing the code of another. Second, reuse is first an economic phenomenon and second a technical approach, so something has to be useable (and used) before it can be reused. Good reuse may be a natural consequence of good domain analysis and design, and inheritance plays a very small role in it, if any. Third, inheritance obfuscates code. Even they it did aid reuse, derived classes end buried deep in inheritance hierarchies. Inheritance hierarchies have a habit of becoming unmanageably deep and messy beyond the point of convenient utility. In a nutshell, the preferred approach to code reuse is just to encapsulate an object of one existing class inside of the new one we're building. Generics also had their heyday as a harbinger of code reuse but they jury is out on this. The famous and well-designed Standard Template Library (STL) of C++ has come under attack for not being suitable to the different performance profiles that different applications require.
Fourth, inheritance was originally interwoven with genericity in early OO programming languages. Smalltalk in particular used inheritance as a way of documenting the fact that one object could be generic with respect to another. Base class objects were more generic in that they were less refined, carried less information, and were more general than their derived class counterparts. A derived class object could do anything a base class object could do and, if designed well, could be used anywhere a base class object was expected. So class `Rectangle` might be a generic base class useable by any programmer in the general public, with derived classes `Square` and `GoldenRectangle` for use by ice-cube makers and artists, respectively. This notion of genericity is tied up with how we classify things in the real world, which we want to express in our programs.
Things got out of hand when languages attempted to extend this idea into complex design. Inheritance nominally leads to hierarchical structures, with a root base class at the top with a small collection of derived classes, each of which may (or may not) have its own derived classes, and so forth. A hierarchical organization of classes is equivalent to a Venn diagram with no overlap between circles: only total enclosure of one circle by another. Within an enclosing circle, enclosed circles are disjoint. The problem is that real-world classification is more complex than can be expressed that way, particularly as regards how we organize objects into classes according to their use — i.e., their place in use cases. So if we have the set of Men and set of Women and set of Other, as well as the set of drivers and non-drivers, we cannot reasonably use classes and inheritance to capture our mental model of this system.
The canonical failure mode can be demonstrated with a naive use of inheritance to describe the concepts of Vehicle, Boat, Plane, and SeaPlane:

Let's say that a vehicle must have a registration. How many registrations does a Seaplane have? Well, it might have two: one with the aviation authority and the other with the marine authority. Let's say that a Plane has a seat and a Boat has a seat. How many seats does a Seaplane have? For reasons of this problem (called the "diamond problem" after the rough shape of the graph) and other related problems, multiple inheritance has not thrived. The problem is that each of the parents talks about the inherent properties of the object by which we classify it, rather than talking about the behaviours we can expect from each of these entities which form a strong base for how we classify them. (Children, by the way, are apt to classify things by their properties at an early age. If a child learns that something with four legs is a horse, and the four legs are what make an impression on the child, then the child is apt to also call a dog a horse when seeing it for the first time.)
The world _is_ more complex than hierarchy alone can describe. However, we are trying to view the word in terms of the objects and the external services they provide, rather than attempting to manage our understanding of them in terms of understanding the details of their private innards. We already have a way to organize classes into multiple hierarchies: interfaces. We do not use inheritance for that sort of organization.
What is inheritance good for, then? It's a good question. The **trygve** environment makes limited use of inheritance itself, and it's for the sake of convenient code reuse. There are a few utility scripts like `assert` that live in class `Object`. Every **trygve** class is implicitly derived from class `Object`. This is sometimes useful if we want to manage a collection of totally unrelated things: we can put anything into an object declared as `Set