Skip to main content

Empty Set

rascal-0.34.0

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);
|lib://rascal/org/rascalmpl/library/Set.rsc|(6335,367,<277,0>,<291,38>): EmptySet()
at *** somewhere ***(|lib://rascal/org/rascalmpl/library/Set.rsc|(6335,367,<277,0>,<291,38>))
at getOneFrom(|prompt:///|(11,1,<1,11>,<1,12>))
ok

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
>>>>>>> println(getOneFrom(S));
>>>>>>>catch EmptySet():
>>>>>>> println("Cannot apply getOneFrom to empty set");
Cannot apply getOneFrom to empty set
ok