Intersection type

Such a value can be safely passed as argument to functions expecting either of the two types.

For example, in Java the class Boolean implements both the Serializable and the Comparable interfaces.

In comparison, underlying objects of intersection types are not necessarily composite.

[2] For example, if number => number is the type of function taking a number as an argument and returning a number, and string => string is the type of function taking a string as an argument and returning a string, then the intersection of these two types can be used to describe (overloaded) functions that do one or the other, based on what type of input they are given.

Contemporary programming languages, including Ceylon, Flow, Java, Scala, TypeScript, and Whiley (see comparison of languages with intersection types), use intersection types to combine interface specifications and to express ad hoc polymorphism.

Complementing parametric polymorphism, intersection types may be used to avoid class hierarchy pollution from cross-cutting concerns and reduce boilerplate code, as shown in the TypeScript example below.

[3] Remarkably, program termination can be precisely characterized using intersection types.

[4] TypeScript supports intersection types,[5] improving expressiveness of the type system and reducing potential class hierarchy size, demonstrated as follows.

The following program code defines the classes Chicken, Cow, and RandomNumberGenerator that each have a method produce returning an object of either type Egg, Milk, or number.

Additionally, the functions eatEgg and drinkMilk require arguments of type Egg and Milk, respectively.

The following program code defines the ad hoc polymorphic function animalToFood that invokes the member function produce of the given object animal.

Ideally, animalToFood should not be applicable to any object having (possibly by chance) a produce method.

Finally, the following program code demonstrates type safe use of the above definitions.

The above program code has the following properties: The above minimalist example can be realized using inheritance, for instance by deriving the classes Chicken and Cow from a base class Animal.

The above minimalist example already shows that duck typing is less suited to realize the given scenario.

The above example can be realized using duck typing, for instance by introducing a new field argumentForAnimalToFood to the classes Chicken and Cow signifying that objects of corresponding type are valid arguments for animalToFood.

The above example can be realized using function overloading, for instance by implementing two methods animalToFood(animal: Chicken): Egg and animalToFood(animal: Cow): Milk.

Other programming languages, such as Java, require distinct implementations of the overloaded method.

It would require each animal class to implement an accept method accepting an object implementing the interface AnimalVisitor (adding non-local boilerplate code).

The function animalToFood would be realized as the visit method of an implementation of AnimalVisitor.

If the behavior of a function can be specified precisely by either a unified interface, parametric polymorphism, or duck typing, then the verbose nature of intersection types is unfavorable.

Therefore, intersection types should be considered complementary to existing specification methods.

is the type which results from replacing all occurrences of the term variable

Scala supports type declarations [7] as object members.

[8] For example, the following program text defines a Scala trait Witness, which can be used to implement the singleton pattern.

[9] The above trait Witness declares the member T, which can be assigned a type as its value, and the member value, which can be assigned a value of type T. The following program text defines an object booleanWitness as instance of the above trait Witness.

In the above example, the object booleanWitness can be assigned the dependent intersection type

The object booleanWitness has the member T that is assigned the type Boolean as its value.

Alternatively, the above minimalistic example can be described using dependent record types.

This notion is also called implicit Pi type,[11] observing that the argument