Empty List
rascal-0.34.0
Synopsis
Illegal operation on an empty list.
Types
data RuntimeException = EmptyList();
Usage
import Exception;
(only needed when EmptyList
is used in catch
)
Description
Rascal provides many operations and functions on lists, see list values and list functions. This error is generated when a function or operation cannot handle the empty list.
Remedies:
- Guard the function or operation with a test on the empty list (isEmpty) and take alternative action in that case.
- Catch the
EmptyList
yourself, see try catch.
Examples
Import the List
library and introduce L
with an empty list as value:
rascal>import List;
ok
rascal>L = [];
list[void]: []
Taking the head of an empty list gives an error:
rascal>head(L);
|lib://rascal/org/rascalmpl/library/List.rsc|(4224,9,<162,31>,<162,40>): EmptyList()
at head(|lib://rascal/org/rascalmpl/library/List.rsc|(4193,45,<162,0>,<162,45>))
at $shell$(|prompt:///|(0,8,<1,0>,<1,8>))
ok
This is the case when taking the tail as well:
rascal>tail(L);
|lib://rascal/org/rascalmpl/library/List.rsc|(17252,9,<694,37>,<694,46>): EmptyList()
at tail(|lib://rascal/org/rascalmpl/library/List.rsc|(17215,51,<694,0>,<694,51>))
at $shell$(|prompt:///|(0,8,<1,0>,<1,8>))
ok
We can also catch the EmptyList
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(head(L));
>>>>>>>catch EmptyList():
>>>>>>> println("Cannot take head of empty list");
Cannot take head of empty list
ok