Type Implementations

From dis-Emi-A

Jump to: navigation, search


This page is a review of the mechanisms which exists, in use or in theory, that enable a language to specify the type of data.

Refer to Types for the abstract discussion on what a type is.

Contents

Classes / Inheritance

C++/Java and many others implement a class based system of typing. Typical to this system is a series of base classes and subclasses which modify the behaviour of the object in some fashion. This is referring strictly to these inheritance relationships and does not include interfaces.

The major advantage is the reduction of code duplication and the specialization of types through the use of polymorphism, in particular virtual functions.

The major disadvantage is the unclarity as to what it actually means to be a derived class. Consider a class B derived from class A. Class A has a specific contract for all of its members (data and functions), now class B can do only one of two things:

  1. maintain the same contract
  2. modify the behaviour thereby altering the contract

In the first case one can argue that if the contracts are properly specified and B maintains them, then B is actually the exact same class as A, and nothing has been achieved. In the second case we have a problem that any use of class A will now have broken assumptions if it is given class B instead.

This significant problem is usually overcome with quasi-contracts applied to the member functions. That is, they are given a rather vague description of what is done, or specifically state that the behavious is defined by the subclass. This is in turn managed by attentative programming in the use of these objects.

Note: The disadvantage is limited to systems using such constructs. Should derived types always be treated as fully distinct classes then inheritance becomes a great way to reduce code duplication without producing type incompatibilities. In this regards however it is functioning more as a parameterized type, and not really a derived type.

Interfaces

Interfaces provide a manner of separating the implementation from the function of data. This technique is available in many languages, though some have special keywords for it (interface in Java).

This provides a great degree of abstraction and allows a fixed contract on the interface by which the caller can be certain is met.

The drawback is that the data must always be treated as a blackbox and the caller is limited to the set of exposed functions. This weakness is particularily strong when attempting to do cloning or serialization of such data. Often this is overcome with dynamic casting to get another type out of the object.


Prototyping

Rather than creating explicit classes it is also possible to create subtypes by simply cloning a template instance and adding or removing members from it. This technique is used by JavaScript.

This technique is quick and easy to use, allowing very simple overriding of members to get a new type.

A disadvantage is the proliferation of types and the resulting difficulty in managing those types.


Keywords

Mainly used for expressing aspects other than structure, keywords are in use in many languages such as public, private, const, mutable, et cetera.

Ada limits

Ada allows very easy creation of new types based on limits / constraints. This allows catching of many conversion/assignment errors since each subtype is distinct. The limitation however is that each type is distinct and assignment cannot occur even if it is logically compatible.

Parametric Types

Many data structures are abstract concepts are define a pattern of usage rather than define data themselves. In order to be used however they need to work on actual data, and the combination of the abstract type and the parameter types are considered the combined type.

Parametric types are achived with specific keywords (such as template in C++), or via polymorphism and inheritance. Such a technique can also be achieved with dynamic typing.

Such typing is a great way to reduce code duplication and maintain compile-time type checking, though the manner of implementation may yield either compiled code bloat (distinct compilations of each use) or potential performance limitations (a unified output working on dynamic types).

Dynamic Types

Rather than statically fixing all the types of the data at compile time those types can be left to be fully determined at run-time, and in particular only as needed.

A language such as Python does this, though one could also say that Java can do this as well if you pass everything as an Object.

Such a system should automatically provide a parametric system as the operations needed are determined at runtime, leaving all intermediate code (between the user and the data) to be generic.

The disadvantages include the lack of compile-time type checking and a potential performance reduction at run-time (in comparison to static types).

Note that is distinct from variant typing; in dynamic typing each instance has one specific type, it just may not be known at compile time.

Variant Typing

Variant typing indicates that instances can assume alternate structures based on how they are used. That is, should you request a number from an instance you'll get a number, but should you request a string you'll get a string.

PHP has a quasi-variant typing system (it does so few auto-conversions it is hard to say it is really variant typed). The more common variant typing seen is with command-line variables or properties vile where they remain as strings until parsed until something specific.

One of the system I created, the context system had partial variant typing.

The advantages is placing emphasis on values rather than types, allowing the programmer to simply pass around whatever they consider the best representation for an object. It also allows easy interfacing to other systems.

The major disadvantage is the lack of compile-time type checking (though there is a way to make this possible), and the need to write a large number of type converters to make it useful.

It should also be noted that a particular lacking aspect to variant is typing is that usually only the most fundamental types actually have variant forms, and all complex types only have a single form, making the variant typing degrade to simply dynamic typing.

Personal tools