4

I came from a highly functional and procedural background in programming, and never knew that a type is the same as an interface.

As in the Design Patterns book by GoF, it says:

A type is a name used to denote a particular interface. We speak of an object as having the type "Window" if it accepts all requests for the operations defined in the interface named "Window." An object may have many types, and widely different objects can share a type. (p. 13)

The surprising thing is, I thought of type as char (like a character, or 1 byte), or int (a word, or 4 bytes or 8 bytes), or a pointer to character (a string in C language) before. Maybe even a struct with x and y as coordinate of a point, or an array, as a type, but I never thought of a type being "an interface".

So it looks like a Car object can be of the type Moveable and Soundable, and a Dog object can be of the type Moveable and Soundable, while a Circle object may be of the type Moveable only, until we decide that a Shape object also need to give out sound, when a user clicks on it, and we let the Shape class implement the Soundable interface and now a Circle object is also of the type Soundable?

I wonder when and how it happened? Is it actually said to be so by the GoF book for the first time in 1994 when the book got published? Or is it actually existing idea that came from long time ago?

It actually sound exactly the same as Duck Typing, but Duck Typing seems like a new concept that began about in 2003 in the Python and Ruby community, not like an idea that was in 1994 or earlier.

  • 4
    Although the name "duck typing" was probably invented in 2000, the dynamic type system underlying it has been around at least since Smalltalk (1970ies) if not longer. – Sebastian Redl Jan 3 '16 at 10:28
  • 4
    In that context, I don't interpret interface as literally an interface in OOP, but more abstractly, the manner in which you interact with something. – Brandon Jan 3 '16 at 19:14
11

The idea of having a type defined by it's interface (in a general meaning, including syntax and semantics) is well known under the name Abstract Data Type. The oldest reference to this term I could find is here, ("Programming with Abstract Data Types", B. Liskov and S. Zilles), it refers back to 1974. According to this paper, which contains a full historic overview, that was exactly the time when the idea of type abstraction was invented.

The ADT concept has not much to do with "duck typing" or "dynamic typing". You can implement ADTs easily in a statically typed language like, for example, C++ by utilizing classes.

  • 1
    (The best-known ADT in C++ is probably Iterator (and its refinements), modeled by pointers and user-defined-types.) – Deduplicator Jan 3 '16 at 11:23
  • 1
    @Deduplicator: I guess any data class which hides its internal implementation behind a public interface can be seen as an ADT. Iterators are surely belonging into that category, but I they would not be my first choice for a simple example to explain the concept to someone else. – Doc Brown Jan 3 '16 at 11:37
  • You have a point, and I wonder why I didn't simply name the concept "Container" or the like... – Deduplicator Jan 3 '16 at 11:41
  • No discussion about type abstraction could be complete without citing Cook and/or Liskov. Well done, sir! – Jörg W Mittag Jan 3 '16 at 12:07
6

Type theory actually predates the Gang of Four by about a hundred years at the least. Russell and Frege developed what is called "Type Theory" around the turn of the 20th century. What was very important in type theory was that every operation between instance of two (or more) types resulted in an easily calculable result type.

For computer programmers, this was very valuable, because the more you know about something, the more it can be optimized. If the "type" of a variable describes a great deal about how it behaves, the compiler can emit efficient code without needing to know the values of those variables until run time.

The connection between interfaces and types was natural for the era of the GoF. If the design patterns could only be implemented with high level features, they wouldn't go far. The current standard process was to boil as much valuable information into types as possible, so the compiler can help you. So it became very natural to treat interfaces as types.

  • any body has answer for, what if a Fixnum + Fixnum, then the type can be either a Fixnum or Bignum type? The Fixnum is limited to 32 or 64 bit integers, while Bignum is arbitrary-length integer. Then the return type is not predictable – 太極者無極而生 Jan 4 '16 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.