# Listing of doc/example-scripts/bagel.tcl

This example is a straight translation of the OTcl Tutorial http://www.isi.edu/nsnam/otcl/doc/tutorial.html to NX. It serves as a very short intro to the basic elements of scripting with NX and provides a comparison study to OTcl.

Suppose we need to work with many bagels in our application. We might start by creating a Bagel class.

We can now create bagels and keep track of them using the info method.

Of course, bagels don’t do much yet. They should remember whether they’ve been toasted. We can create and access an instance variable by defining an property for the class. All instance variables are per default public in the sense of C++.

Since abagel was created before the definition of the property we have to set the default value for it using the setter method. Again, the info method helps us keep track of things.

But we really want them to begin in an untoasted state to start with.

Our bagels now remember whether they’ve been toasted. Let is recreate the first one.

Now we’re ready to add a method to bagels so that we can toast them. Methods have an argument list and body like regular Tcl procs. Here’s the toast method.

The defined methods can be queried with info. We see as well the setter method for the variable toasted.

Aside from setting the toasted variable, the body of the toast method demonstrates how to access instance variables by using a leading colon in the name.

We invoke the toast method on bagels in the same way we use the info and destroy methods that were provided by the system. That is, there is no distinction between user and system methods.

Now we can add spreads to the bagels and start tasting them. If we have bagels that aren’t topped, as well as bagels that are, we may want to make toppable bagels a separate class. Let explore inheritance with these two classes, starting by making a new class SpreadableBagel that inherits from Bagel. A SpreadableBagel has an property toppings which might have multiple values. Initially, toppings are empty.

Let’s add a taste method to bagels, splitting its functionality between the two classes and combining it with next.

Of course, along come sesame, onion, poppy, and a host of other bagels, requiring us to expand our scheme. We could keep track of flavor with an instance variable, but this may not be appropriate. Flavor is an innate property of the bagels, and one that can affect other behavior - you wouldn’t put jam on an onion bagel, would you? Instead of making a class hierarchy, let’s use multiple inheritance to make the flavor classes mixins that add a their taste independent trait to bagels or whatever other food they are mixed with.

Well, they don’t appear to do much, but the use of next allows them to be freely mixed.

For multiple inheritance, the system determines a linear inheritance ordering that respects all of the local superclass orderings. You can examine this ordering with an info option. next follows this ordering when it combines behavior.

We can also combine our mixins with other classes, classes that need have nothing to do with bagels, leading to a family of chips.