Skip to main content

module UUID

rascal-0.28.2

Usage

import util::UUID;

function uuid

Synopsis

generates a unique identifier shaped as a loc

Description

This function generates a UUID, see http://en.wikipedia.org/wiki/Universally_unique_identifier. Since UUIDs are useful to assign an opaque and unique identity to data, the function returns a Location (which is the preferred representation for encoding identities in Rascal)

Examples

rascal>import util::UUID;
ok

The uuid() function generates a location with the authority showing the literal canonical UUID string

rascal>uuid()
loc: |uuid://e69087af-d54d-4aad-b15c-dc1c22173040|

Use it to relate identies to data objects, as in this example which adds a field to a relation:

rascal>myData = { <i,i*i> | i <- [1..11] }; 
rel[int,int]: {
<10,100>,
<7,49>,
<1,1>,
<3,9>,
<9,81>,
<2,4>,
<4,16>,
<6,36>,
<5,25>,
<8,64>
}
rascal>rel[int n, int square, loc id] myUniqueData = { <i,j,uuid()> | <i,j> <- myData };
rel[int n,int square,loc id]: {
<1,1,|uuid://8e8ee325-6fe9-4e43-915d-217f1bd2522b|>,
<5,25,|uuid://42c758ee-ce18-4000-9c08-21047514e0d0|>,
<6,36,|uuid://5deef573-273d-44fd-a07f-b020bb5dd166|>,
<2,4,|uuid://ff30f4af-8ea6-4fbb-a43a-7e44ab3ca542|>,
<3,9,|uuid://d25daf14-3963-4997-af99-7453052c5b37|>,
<8,64,|uuid://ef906c85-0368-42eb-ad68-adc4c5c4581a|>,
<4,16,|uuid://96bbd455-a4f2-40cd-8a59-774576d828ee|>,
<9,81,|uuid://7998e4d7-701b-4811-bf32-4ebcbcd016ba|>,
<7,49,|uuid://92c34475-78d0-4315-9dd1-1567ef9f84f7|>,
<10,100,|uuid://9b2f8dd2-743b-4145-8ca4-e65615c2780a|>
}
rascal>map[tuple[int i, int j] t, loc id] myUniqueMap = (<i,j>:uuid() | <i,j> <- myData );
map[tuple[int i,int j] t, loc id]: (
<6,36>:|uuid://f9f785e3-a03c-4eac-b872-938b995772d1|,
<2,4>:|uuid://13b92be8-5333-4b70-b85c-e205de4b0bb9|,
<7,49>:|uuid://99d00ea6-e6b0-48a8-9bcc-6611f638ec35|,
<9,81>:|uuid://d4a5c8ae-d6fc-48fd-b760-cf8ac88e389a|,
<8,64>:|uuid://72e32c51-db95-41a8-9f86-beefd2ea50ef|,
<5,25>:|uuid://1da29883-5677-46e0-a4ec-0bc77150e589|,
<4,16>:|uuid://d746c290-1fa9-40b0-8224-f15e892cf0f7|,
<1,1>:|uuid://95e4cf42-bb77-4900-9510-c003ad7cf9c5|,
<10,100>:|uuid://c1b00777-4d97-4381-9223-576533b08b84|,
<3,9>:|uuid://e7c422d5-a4fc-4ff4-94ab-bcd71984026a|
)

Note how uuid() should always generate a fresh value:

rascal>assert uuid() != uuid(); 
bool: true

Benefits

  • Locations are used for identifying program elements or model elements in Rascal. The uuid() function provides an quick-and-easy way of acquiring such an identity without having to design a naming scheme.

Pitfalls

  • UUIDs are a quick and dirty way of identifying data which may lead to hard to debug code. A naming scheme for Locations is better because it generates human readable Locations which carry meaning. For example consider the difference in readability between these two values: |uuid://47fdcd64-4fd0-41a1-8aa3-61c5b272c3fc| and |java+class:///java/lang/Object|. Both may lead to the same results in your computation, but if we print either of them out, one of them is opaque and the other is transparent. A transparent naming scheme is preferable for debugging purposes.
loc uuid()

function uuidi

Synopsis

see [uuid], this function does the same except return the UUID as an int.

Pitfalls

  • beware that this integer is almost guaranteed to use 128 bits, so communicating it outside of Rascal should not be done via a Java 32-bit integer.
int uuidi()