Skip to main content

Empty List

rascal-0.40.17

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);
|jar+file:///home/runner/.m2/repository/org/rascalmpl/rascal/0.40.17/rascal-0.40.17.jar!/List.rsc|(4213,9,<161,31>,<161,40>): EmptyList()
at head(|jar+file:///home/runner/.m2/repository/org/rascalmpl/rascal/0.40.17/rascal-0.40.17.jar!/List.rsc|(4182,45,<161,0>,<161,45>))
at $shell$(|prompt:///|(0,8,<1,0>,<1,8>))
ok

This is the case when taking the tail as well:

rascal>tail(L);
|jar+file:///home/runner/.m2/repository/org/rascalmpl/rascal/0.40.17/rascal-0.40.17.jar!/List.rsc|(17241,9,<693,37>,<693,46>): EmptyList()
at tail(|jar+file:///home/runner/.m2/repository/org/rascalmpl/rascal/0.40.17/rascal-0.40.17.jar!/List.rsc|(17204,51,<693,0>,<693,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