Skip to main content

Empty Map

rascal-0.34.0

Synopsis

Illegal operation on an empty map.

Types

data RuntimeException = EmptyMap();

Usage

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

Description

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

Remedies:

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

Examples

Import the Map library and introduce M with an empty map as value:

rascal>import Map;
ok
rascal>M = ();
map[void, void]: ()

Trying to get an arbitrary value from it gives an error:

rascal>getOneFrom(M);
|lib://rascal/org/rascalmpl/library/Map.rsc|(1872,385,<78,0>,<91,41>): EmptyMap()
at *** somewhere ***(|lib://rascal/org/rascalmpl/library/Map.rsc|(1872,385,<78,0>,<91,41>))
at getOneFrom(|prompt:///|(11,1,<1,11>,<1,12>))
ok

We can also catch the EmptyMap 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(M));
>>>>>>>catch EmptyMap():
>>>>>>> println("Cannot use getOneFrom on empty map");
Cannot use getOneFrom on empty map
ok