Empty Map
rascal-0.41.2
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
EmptyMapyourself, 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);
|std:///Map.rsc|(1872,385,<78,0>,<91,41>): EmptyMap()
at *** somewhere ***(|std:///Map.rsc|(1872,385,<78,0>,<91,41>))
at getOneFrom(|prompt:///|(11,1,<1,11>,<1,12>))
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
|1 >>>> println(getOneFrom(M));
|2 >>>>catch EmptyMap():
|3 >>>> println("Cannot use getOneFrom on empty map");
Cannot use getOneFrom on empty map
ok