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://091f5a87-954e-4a0c-9458-3ae6a98e709f|

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://9c2dc094-d1f8-46ab-ae1f-1ac38d709bc9|>,
<10,100,|uuid://6eb75547-4992-47aa-be86-3dc036200799|>,
<5,25,|uuid://b2baf930-e081-45f4-9fb7-859c9551b062|>,
<2,4,|uuid://de83e979-8306-47bd-aef6-ca98c6abd701|>,
<6,36,|uuid://0b748681-53ba-416d-9b78-a1de1f6fa340|>,
<3,9,|uuid://9439127b-f41f-4db0-acf3-b89727c9d430|>,
<1,1,|uuid://192aaccf-ae63-4dda-88b8-6822c1bf6b1b|>,
<7,49,|uuid://15aebfdf-67b9-465e-bdd7-b3513fdf6316|>,
<9,81,|uuid://4b945ac1-48b8-4248-91af-5677b37578ef|>,
<8,64,|uuid://e29066f1-0a06-4053-a841-b7514b02dfbc|>
}
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://abe59cc5-b22b-419b-ac84-53617a16ef31|,
<2,4>:|uuid://b97e79be-0139-4913-bc20-8072d6670313|,
<7,49>:|uuid://2b93a3dd-1573-4968-a3b9-4497931052fa|,
<9,81>:|uuid://54a30fcd-8005-43d9-8923-d1cd44789d8a|,
<8,64>:|uuid://1e65838a-2375-4494-8098-9d9c1f902f41|,
<5,25>:|uuid://f23bf941-014b-433f-9faf-6c3ad4f1ecbc|,
<4,16>:|uuid://d738fc07-b264-4777-a614-211acb858028|,
<1,1>:|uuid://3d4dc6c5-8c8d-48d2-a2ef-afae8fb14d6f|,
<10,100>:|uuid://57bc25a9-53be-45cd-b51a-6dd438754de6|,
<3,9>:|uuid://b4eb0165-bf25-4470-8482-0c99d1b2d2bf|
)

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.