![]() |
|
Navigation |
Synopsis Illegal operation on an empty list.
Function
data RunTimeException = EmptyList();
Usage
import Exception;
Description Rascal provides many operations and functions on lists, see Rascal:Values/List and Rascal:Prelude/List.
This error is generated when a function or operation cannot handle the empty list.
Remedies:
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);
|rascal://List|(3636,1037,<170,0>,<209,51>): EmptyList()
at *** somewhere ***(|rascal://List|(3636,1037,<170,0>,<209,51>))
at head(|stdin:///|(5,1,<1,5>,<1,6>))
This is the case when taking the tail as well:
rascal>tail(L);
|rascal://List|(24027,958,<1194,0>,<1238,57>): EmptyList()
at *** somewhere ***(|rascal://List|(24027,958,<1194,0>,<1238,57>))
at tail(|stdin:///|(5,1,<1,5>,<1,6>))
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 ![]() |