Skip to main content

Empty Set

rascal-0.41.2

Synopsis

Illegal operation on an empty set.

Types

data RuntimeException = EmptySet();

Usage

import Exception; (only needed when EmptySet is used in catch)

Description

Rascal provides many operations and functions on sets, see set values and set functions. This error is generated when a function or operations cannot handle the empty set.

Remedies:

  • Guard the function or operation with a test on the empty set (isEmpty) and take alternative action in that case.
  • Catch the EmptySet yourself, see try catch.

Examples

Import the Set library and introduce S with an empty set as value:

rascal>import Set;
ok
rascal>S = {};
set[void]: {}

Taking an element from an empty set gives an error:

rascal>getOneFrom(S);
|std:///Set.rsc|(6272,1208,<273,0>,<303,38>): EmptySet()
at *** somewhere ***(|std:///Set.rsc|(6272,1208,<273,0>,<303,38>))
at getOneFrom(|prompt:///|(11,1,<1,11>,<1,12>))

We can also catch the EmptySet error. First import the Rascal exceptions (which are also included in Prelude) and IO:

rascal>import Exception;
ok
rascal>import IO;
ok
rascal>try
|1 >>>> println(getOneFrom(S));
|2 >>>>catch EmptySet():
|3 >>>> println("Cannot apply getOneFrom to empty set");
Cannot apply getOneFrom to empty set
ok