Perfect Developer basic tutorial 4 This page last modified 2011-10-30 (JAC)

The toString method

There is just one function that is automatically defined for every class you declare.  That function is called toString and it takes no parameters. Its job is to return a textual representation of any object of that class. However, the default version isn't very useful because it prints the same string regardless of the values of the class attributes. If you intend to use toString at all, you will certainly want to customize it for your class. We can customize toString for our Book class by placing the following declaration in the interface section:

redefine function toString: string
  ^= "'" ++ title ++ "' by " ++ interleave(authors.permndec, " and ");

Recall that the operator ++ between sequences means concatenation (and strings are just sequences of characters). The permndec member function of class set of X yields the members of the set as a sequence in nondecreasing order. The global function interleave takes two arguments: a seq of seq of X (e.g. a seq of seq of char, which is the same as a seq of string) and a seq of X (e.g. a seq of char, or string). It concatenates the elements of the first parameter, inserting the second parameter between each pair of elements. So in this case, it concatenates all the authors together, inserting the string " and " between them.

Given a Book such as myFavoriteBook you can now use the expression myFavoriteBook.toString to get a value that you can print or display to represent the book.

Sometimes you will want to provide a toString method that takes some sort of formatting parameter, so that you can generate different textual representations of the same word.
For example, you might want to make our program support alternative output languages. You can provide alternative versions of toString (or any other function) provided that the different versions take different numbers or different types of parameters. Here's a version of toString that lets the caller pass words meaning "by" and "and" in a chosen language:

function toString(byWord, andWord: string): string
  ^= "'" ++ title ++ "' " ++ byWord ++ " " ++ interleave(authors.permndec, " " ++ andWord ++ " ");

That's getting rather hard to read and understand, so here's an equivalent version that uses let-declarations to break the complex expression into simpler parts:

function toString(byWord, andWord: string): string
  ^= ( let quotedTitle ^= "'" ++ title ++ "'";
       let byWithSpaces ^= " " ++ byWord ++ " ";
       let andWithSpaces ^= " " ++ andWord ++ " ";
       quotedTitle ++ byWithSpaces ++ interleave(authors.permndec, andWithSpaces)
     );

Now let's define the parameterless toString function in terms of this new one:

redefine function toString: string
  ^= toString("by", "and");

That's enough theorising; to try this out, let's look at how to write a simple test harness.

Next:  Creating main

 

Save My Place Glossary Language Reference Manual
Tutorials Overview Main site   
Copyright © 1997-2012 Escher Technologies Limited. All rights reserved. Information is subject to change without notice.