Skip to main content

Map Subscription

rascal-0.40.17

Synopsis

Retrieve a value by its key in map.

Syntax

Exp₁ [ Exp₂ ]

Types

Exp₁Exp₂Exp₁ [ Exp₂ ]
map[T₁, T₂]T₁T₂

Description

Map subscription uses the value of Exp₂ as key in the map value of Exp₁ and returns the associated value. If this key does not occur in the map, the exception NoSuchKey is thrown.

Examples

Introduce a map, assign it to colors, and retrieve the element with index "trumps":

rascal>colors = ("hearts":"red", "clover":"black", 
>>>>>>> "trumps":"black", "clubs":"red");
map[str, str]: ("hearts":"red","trumps":"black","clover":"black","clubs":"red")
rascal>colors["trumps"];
str: "black"
---
black
---

Explore some erroneous subscription expressions:

rascal>colors[0];
|prompt:///|(7,1,<1,7>,<1,8>): Expected str, but got int
Advice: |https://www.rascal-mpl.org/docs/Rascal/Errors/CompileTimeErrors/UnexpectedType|
ok
rascal>colors["square"];
|prompt:///|(7,8,<1,7>,<1,15>): NoSuchKey("square")
at $shell$(|prompt:///|(0,17,<1,0>,<1,17>))
ok