![]() |
![]() |
![]() |
||||||||||||||
| Controlling Variable Bindings < TMQL Introduction < < Home | ||||||||||||||||
|
Controlling Variable BindingsAs we have seen above, variables can be bound to values. Used naively, this can lead to incorrect queries. As an example let us find all two albums which share the same producer. In a first attempt we write: select $album1, $album2 where is-produced-by ($album1: production, $producer: producer) & is-produced-by ($album2: production, $producer: producer)If you have worked with declarative languages before, you may immediately spot the problem: For the TMQL processor $album1 and $album2 are completely different variables; the variables might be bound to the same or to different value for the same producer, the processor does not care. This does not work for us if we want different albums. The usual escape hatch is to have something like this: select $album1, $album2 where is-produced-by ($album1: production, $producer: producer) & is-produced-by ($album2: production, $producer: producer) & $album1 != $album2Not only is this ugly as hell, in 100% - ε of all cases developers will forget to add this (I know I will). And it does not look too good if you have to compare three or more such variables. TMQL has a rather eccentric way to fine-control when variables are allowed to match anything or when they must be bound to something different: select $album, $album' where is-produced-by ($album : production, $producer: producer) & is-produced-by ($album': production, $producer: producer)Now we have used two variables which only differ in their name by the number of primes (') appended. TMQL treats them as two distinct variables, but with the additional semantics that― within one and the same binding―they cannot be bound to the same value. There is no limit to the number of primes, so should we―by a bizarre twist of fate or customer requirements― need three different albums, this can be achieved nicely: select $album, $album', $album'' where is-produced-by ($album : production, $producer: producer) & is-produced-by ($album' : production, $producer: producer) & is-produced-by ($album'': production, $producer: producer)
|
|||||||||||||||