Types - rurban/ponyc GitHub Wiki

Overview

Pony has a comprehensive type system that allows for complex types to be composed on the fly very easily. All data used in Pony consists of objects and all fields and variables contain references to objects.

Explicit types

Objects are instances of explicit types.

Class

Classes are the basic unit of data usage. They can contain any number of fields, be that a single field, multiple fields or even no fields at all. All data fields within classes are private and accessable only to code within that class.

Classes may contain functions which can be called on instances of the class. They may also contain constructors which can be called to create instances of the class. Constructors are called on the class.

Actor

Actors are the basic unit of concurrency. They are similar to classes, but may also have behaviours which receive asynchronous messages from other parts of the program. Behaviours can be thought of as functions that are invoked asynchronously, return immediately (ie before the execution of the function has actually occurred) and return no value. The behaviour execution will occur at some later point, possibly in parallel with the calling code.

Data type

User defined data types are like classes but they cannot have fields and there is only ever a single instance of each data type. Data types may have functions, but not behaviours or constructors. Every data type has an implicit constructor that returns the single instance.

TODO: This needs expanding, but I'm currently unsure how to talk about the arithmetic types, which behave differently.

Object literal

Objects may also be created directly using an object literal. Such an object is identical to a class instance, except that it has no named class type.

Object literals are syntactic sugar provided to allow more compact code. Creating an object literal is exactly the same as writing a new class with a new name, instantiating that class in exactly one place in the code and never referring to that class name anywhere else.

Interfaces

TODO: Trait and structrual. Leave this until I've worked out what the difference is.

Compound types

Union

A union type combines 2 or more others types. An instance of a union type may be an instance of any of the contained types.

For example, in a linked list implementation it is necessary to have a reference to the next node, but there may not be another node. Many languages have a special value assignable to pointers / references to indicate no object, such as NULL or NIL. In Pony the equivalent is the data type None, but it cannot be assigned to every pointer type as C's NULL can. Instead it is necessary to explicitly specify that the reference can be a None in the type. If the type of out linked list node was List then the next node type would be (List | None), ie it might be a List (if there is another node) or it might be None.

The operations that can be performed on a union object are:

  1. It may be assigned to a field, variable, argument or return value of a type that can take all the types contained within the union.

  2. It maybe passed into a match statement. This examines the actual type of the object at runtime and allows it to be treated as whichever type it actually is.

  3. A method may be called on it, as long as every type within the union type provides that method and they all have compatible signatures.

Note that it is not legal to perform operations other than method calling on a union object, even if all types in the union support it. For example, given the union (U16 | U32) it is not permitted to apply the plus operator the an instance of this union, even though all the contained types support it.

Intersection

TODO

Tuple

TODO