Skip to main content

module util::UUID

rascal-0.34.0

Usage

import util::UUID;

function uuid

generates a unique identifier shaped as a loc

loc uuid()

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://4eaa461d-e806-42b2-80a4-54b189df44f7|

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]: {
<4,16,|uuid://780d4b1a-cb1d-45d4-91a5-b974a4edda17|>,
<6,36,|uuid://eb7f416c-ab74-4f2d-932f-e2d564ed3612|>,
<8,64,|uuid://d4e94d57-b477-4be4-a9c8-b53a9698fbe3|>,
<3,9,|uuid://c023364a-a9c6-445e-8b4a-4180e898de35|>,
<1,1,|uuid://6c16e108-47f0-4a5b-92a7-fc2ba83e661c|>,
<2,4,|uuid://26c49e1e-1791-4720-9c58-70b5ff398000|>,
<7,49,|uuid://6acb4a1f-f696-45b5-822b-b3b3cbd0b652|>,
<9,81,|uuid://27b2c1a8-c32e-4cde-bbb1-c38478aff806|>,
<5,25,|uuid://8ea967ed-2a86-41a5-921e-3fc7e121aee7|>,
<10,100,|uuid://4c87b09b-fb11-476c-a697-279b465a008e|>
}
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://54ecc4cc-1ad1-4042-93db-ab120ff2a09a|,
<2,4>:|uuid://c44f5bca-c7fe-440e-8793-36c1d9b09d35|,
<7,49>:|uuid://f7ae1310-6f52-4ec4-a03e-dc8d526cc2fb|,
<9,81>:|uuid://32ee6d63-19c5-41e7-be5a-7eb8aa1904e5|,
<8,64>:|uuid://b7b203ef-513e-4f71-a675-fc3fb889256f|,
<5,25>:|uuid://50de3bc4-5758-4986-96a0-bf8f7211803e|,
<4,16>:|uuid://5603ce92-84b5-417b-9223-687202788a12|,
<1,1>:|uuid://c66ec36f-dd6a-477d-865e-5b13f45a60aa|,
<10,100>:|uuid://60bef235-dddd-4d17-b643-1b90e46c060d|,
<3,9>:|uuid://c708daca-edfa-481d-93f8-160606761422|
)

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.

function uuidi

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

int uuidi()

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.