module util::UUID
rascal-0.40.16
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://51abbc34-f5d9-4b1f-84d5-2343a94ecc5b|
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]: {
<10,100,|uuid://3f352932-aa35-40f5-b875-11a543a2134c|>,
<7,49,|uuid://1f1c6c6b-2b1b-49a3-82b5-124fa7c7b3e8|>,
<1,1,|uuid://2f69d8c6-d62c-43d8-a178-24cd41bb9336|>,
<9,81,|uuid://80c4e26a-4e0b-4674-8564-31623914641e|>,
<5,25,|uuid://ce60e929-c48e-47ed-8980-f52a3ad88485|>,
<6,36,|uuid://ef5e5f81-6562-451b-8de9-77220ac6c184|>,
<3,9,|uuid://295c1c4e-e083-4c08-9f25-6bab51c411c4|>,
<8,64,|uuid://6332f9a0-8671-48f5-a381-26e8215749dc|>,
<4,16,|uuid://c7171508-31f9-41b0-8315-f1ae5be92be0|>,
<2,4,|uuid://874171b0-10d9-4808-a3e2-7f419ae4c49c|>
}
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://6469c890-7170-4cbf-aa14-bfc30994f083|,
<2,4>:|uuid://7cc3e784-7828-4af9-81e1-11720bc544e6|,
<7,49>:|uuid://c357afa7-3518-4f68-b3e7-740e91dd9258|,
<9,81>:|uuid://114f1866-44ad-4351-9dd0-adfbfba9e344|,
<8,64>:|uuid://ccbba82a-fbcb-4498-93dd-3343e5bd061a|,
<5,25>:|uuid://8f45fa7f-4cec-4452-bdd7-843e3507ff95|,
<4,16>:|uuid://127f57b9-9477-40fe-b7b9-3fec8d6ba33f|,
<1,1>:|uuid://330c3a6e-f298-41ce-9b24-5fa2b58009f7|,
<10,100>:|uuid://6921d347-07ad-4399-8f27-019ffd0838c8|,
<3,9>:|uuid://a3ef301c-56da-4933-8934-5f154c41e8af|
)
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.