Ellipsis (computer programming)


In computer programming, ellipsis notation is used to denote ranges, an unspecified number of arguments, or a parent directory. Most programming languages require the ellipsis to be written as a series of periods; a single ellipsis character cannot be used.

Ranges

In some programming languages, a shortened two-dot ellipsis is used to represent a range of values given two endpoints; for example, to iterate through a list of integers between 1 and 100 inclusive in Perl:
In Ruby the ... operator denotes a half-open range, i.e. that includes the start value but not the end value.
In Rust the ..= operator denotes an inclusive range for cases in matches and the .. operator represents a range not including the end value.
Perl and Ruby overload the ".." operator in scalar context as a flip-flop operator - a stateful bistable Boolean test, roughly equivalent to "true while x but not yet y", similarly to the "," operator in sed and AWK.
GNU C compatible compilers have an extension to the C and C++ language to allow case ranges in switch statements:

unsigned int u;
switch

Additionally, GNU C allows a similar range syntax for designated initializers, available in the C language only:

int array = ;

Delphi / Turbo Pascal / Free Pascal:

var FilteredChars: set of ;
var CheckedItems: set of ;

In the Unified Modeling Language, a two-character ellipsis is used to indicate variable cardinality of an association. For example, a cardinality of 1..* means that the number of elements aggregated in an association can range from 1 to infinity.

Parent directory

On Windows and Unix-like operating systems, ".." is used to access the parent directory in a path.

Incomplete code

In Perl and Raku the 3-character ellipsis is also known as the "yada yada yada" operator and, similarly to its linguistic meaning, serves as a "stand-in" for code to be inserted later.
Python3 also allows the 3-character ellipsis to be used as an expressive place-holder for code to be inserted later.
In Abstract Syntax Notation One, the ellipsis is used as an extension marker to indicate the possibility of type extensions in future revisions of a protocol specification. In a type constraint expression like A ::= INTEGER an ellipsis is used to separate the extension root from extension additions. The definition of type A in version 1 system of the form A ::= INTEGER and the definition of type A in version 2 system of the form A ::= INTEGER constitute an extension series of the same type A in different versions of the same specification. The ellipsis can also be used in compound type definitions to separate the set of fields belonging to the extension root from the set of fields constituting extension additions. Here is an example:

Variable number of parameters

C and C++

In the C and C++ programming languages, an ellipsis is used to represent a variable number of parameters to a function. For example:
The above function in C could then be called with different types and numbers of parameters such as:
and
C99 introduced macros with a variable number of arguments.
C++11 included the C99 preprocessor, and also introduced templates with a variable number of arguments, called variadic templates.

Java

As of version 1.5, Java has adopted this "varargs" functionality. For example:
This actually converts to String strings.

PHP

PHP 5.6 supports use of ellipsis to define an explicitly variadic function, where ... before an argument in a function definition means that arguments from that point on will be collected into an array. For example:

function variadic_function
var_dump;

Produces this output:

array

Scheme

The syntax-rules hygienic macro system originally introduced in R4RS uses ... to specify that the proceeding pattern may be matched zero or more times. For example, the following code could be used to implement the standard let* form, recursively in terms of itself, and the more primitive let:


)
body1 body2...)
)
body1 body2...)))))

SRFI 46 was proposed to extend syntax-rules to allow the user to specify an ellipsis identifier. This is useful for disambiguating when ellipsis are use in nested macro definitions. This feature was later included in R7RS.

Multiple dimensions

In Python, the ellipsis is a nullary expression that represents the Ellipsis singleton.
It's used particularly in NumPy, where an ellipsis is used for slicing an arbitrary number of dimensions for a high-dimensional array:

import numpy as np
from typing import NDArray
t: NDArray = np.random.rand
  1. select 1st element from last dimension, copy rest
t.shape
  1. select 1st element from first dimension, copy rest
t.shape

Other semantics

MATLAB

In MATLAB, a three-character ellipsis is used to indicate line continuation, making the sequence of lines
semantically equivalent to the single line
In Raku an actual Unicode ellipsis character is used to serve as a type of marker in a format string.

PHP

Since PHP 8.1, a nullary ellipsis may be used to create a closure from a callable or an object method:

// old style: PHP 8.0 and older
$foo = ;
$fn = Closure::fromCallable;
// new style: PHP 8.1 and newer
$foo = $this->foo;
$fn = strlen;

Python

In Python, the ellipsis can also be used as the first parameter within the collections.abc.Callable type annotation to denote any number of arguments:

from collections.abc import Callable
from typing import TypeAlias, Any
VarFunction: TypeAlias = Callable