Chapter 10.1.1 Generic Programming in Smalltalk | Introduction to Programming Using Java
10.1.1 Generic Programming in Smalltalk
Smalltalk was one of the very first object-oriented programming languages. It is still used today, although its use is not very common. It has not achieved anything like the popularity of Java or C++, but it is the source of many ideas used in these languages. In Smalltalk, essentially all programming is generic, because of two basic properties of the language.
First of all, variables in Smalltalk are typeless. A data value has a type, such as integer or string, but variables do not have types. Any variable can hold data of any type. Parameters are also typeless, so a subroutine can be applied to parameter values of any type. Similarly, a data structure can hold data values of any type.
For example, once you’ve defined a binary tree data structure in SmallTalk, you can use it for binary trees of integers or strings or dates or data of any other type. There is simply no need to write new code for each data type.
Secondly, all data values are objects, and all operations on objects are defined by methods in a class. This is true even for types that are “primitive” in Java, such as integers. When the “+” operator is used to add two integers, the operation is performed by calling a method in the integer class. When you define a new class, you can define a “+” operator, and you will then be able to add objects belonging to that class by saying “a + b” just as if you were adding numbers.
Now, suppose that you write a subroutine that uses the “+” operator to add up the items in a list. The subroutine can be applied to a list of integers, but it can also be applied, automatically, to any other data type for which “+” is defined. Similarly, a subroutine that uses the “<” operator to sort a list can be applied to lists containing any type of data for which “<” is defined. There is no need to write a different sorting subroutine for each type of data.
Put these two features together and you have a language where data structures and algorithms will work for any type of data for which they make sense, that is, for which the appropriate operations are defined. This is real generic programming.
This might sound pretty good, and you might be asking yourself why all programming languages don’t work this way. This type of freedom makes it easier to write programs, but unfortunately it makes it harder to write programs that are correct and robust (see Chapter 8).
Once you have a data structure that can contain data of any type, it becomes hard to ensure that it only holds the type of data that you want it to hold. If you have a subroutine that can sort any type of data, it’s hard to ensure that it will only be applied to data for which the “<” operator is defined. More particularly, there is no way for a compiler to ensure these things.
The problem will only show up at run time when an attempt is made to apply some operation to a data type for which it is not defined, and the program will crash.
SEE MORE:
- Chapter 10.1.2 Generic Programming in C++ | Introduction to Programming Using Java
- Chapter 10.1.3 Generic Programming in Java | Introduction to Programming Using Java
- Chapter 10.1.4 The Java Collection Framework | Introduction to Programming Using Java
- Chapter 10.1.5 Iterators and for-each Loops | Introduction to Programming Using Java
- Chapter 11.2.1 Reading and Writing Files | Introduction to Program ming Using Java
- Chapter 11.2.2 Files and Directories | Introduction to Programming Using Java
- Chapter 11.3.1 Copying a File | Introduc’tion to Program’ming Using Java
- Chapter 11.3.3 Files in GUI Programs | Introduction to Program’ming Using Java
- Chapter 11.4.2 TCP/IP and Client/Server | Introduction to Program ming Using Java
- Chapter 12 Advanced GUI Programming | Images and Resources | Introduction to Program ming Using Java
17 thoughts on “Chapter 10.1.1 Generic Programming in Smalltalk | Introduction to Programming Using Java”