4.1 General
Every value in Buoy has a type.
A type defines the set of values that may be represented together with the operations that may be performed upon those values. The compiler uses type information to verify program correctness, resolve overloaded routines, infer the types of expressions, and generate efficient executable code.
Buoy is a statically typed language. Except where explicitly permitted by the language, the type of every expression is determined during compilation.
4.2 Static Typing
Type checking is performed at compile time.
A program that attempts an operation not defined for the operand types is ill-formed and shall be rejected by the compiler.
Examples of compile-time type errors include:
- assigning a value to an incompatible type
- invoking a routine with an incorrect argument type
- attempting an undefined operator
- returning a value of the wrong type from a function
The compiler should diagnose type errors before code generation.
4.3 Type Categories
The Buoy type system consists of several categories of types.
These include:
- primitive types
- enumeration types
- arrays
- structures
- classes
- interfaces
- delegate types
- generic types
- collection types supplied by the standard library
Each category is described in a subsequent chapter.
4.4 Named Types
A named type is introduced by a type declaration.
Once declared, the type may be referenced wherever a type specification is permitted.
The scope and visibility of a type are determined by the declaration that introduces it.
4.5 Type Identity
Two types are identical if they denote the same declared type or if they are defined by the language to be equivalent.
Distinct declarations introduce distinct types, even when their internal representation is identical, unless otherwise specified by the language.
Type identity is used during assignment, parameter passing, overload resolution, and generic type substitution.
4.6 Type Compatibility
A value may be assigned to a destination only when the source type is compatible with the destination type.
Compatibility may arise through:
- identity
- implicit conversion
- explicit conversion
- generic type substitution
- inheritance or interface implementation, where applicable
The precise compatibility rules are defined throughout this reference.
4.7 Type Inference
In certain contexts, the compiler may infer a type rather than requiring it to be written explicitly.
Type inference never changes the meaning of a program. It merely allows the compiler to determine a type that could otherwise have been written by the programmer.
Inference may occur for:
- local variable declarations
- generic type arguments
- generic routine invocations
- expression evaluation
If more than one type satisfies the language rules, the compiler shall report an ambiguity.
4.8 Value Semantics and Reference Semantics
Some Buoy types exhibit value semantics.
Assignment of a value type copies the value.
Other types exhibit reference semantics.
Assignment of a reference type copies the reference rather than the referenced object.
The semantic category of each built-in and user-defined type is specified by its declaration.
4.9 Default Values
Every type has an associated default value.
When an object or variable is created without an explicit initializer, the implementation supplies the default value appropriate for its type.
The default value of each built-in type is described in the corresponding chapter.
4.10 Generic Types
Generic types define families of related types parameterized by one or more type parameters.
A generic declaration describes the structure and behaviour common to every specialization.
A constructed type is produced by substituting concrete type arguments for the generic parameters.
Generic types are discussed in detail in Chapter 18.
4.11 Type Conversions
A conversion transforms a value from one type to another.
Conversions may be:
- implicit
- explicit
- user-defined
- compiler-generated
An implicit conversion shall never lose information unless explicitly permitted by the language specification.
Conversions that may fail or lose information require explicit programmer intent.
4.12 Type Safety
The primary objective of the Buoy type system is to detect programming errors before execution.
A conforming implementation shall reject any program that violates the static type rules defined by this specification.
Programs accepted by the compiler may assume that every expression has a well-defined compile-time type.
4.13 Summary
The Buoy type system provides the foundation upon which declarations, expressions, and generic programming are built.
Subsequent chapters describe each category of type together with the operations supported by those types.
