A scope combines a namespace, a locale and properties. The locale provides context-sensitive formatting and parsing of numbers and currencies. A scope can also contain variables that are evaluated prior to the launch of a query.
The TAQ compiler automatically creates the initial scope, called the "global" scope because it is the fallback location to find an artifact when a search from within a non-global scope comes up empty. Any additional scope can only be declared in the global scope as nesting of scopes is not allowed.
A scope change is possible when a query progresses from one stage to the next or when a query call occurs. A scope change is signaled by the use of a 2-part name for the target,
Attributes
There is a small set of attributes shared by all scopes. To get the scope name, use the notation
The other attributes relate to locale and are covered in Locales.
Properties
A scope declaration may define one or more properties. To read a property, use the scope keyword with a right arrow -> pointing to the property name. calc-square-miles3.taq has 3 queries which display the area of USA and Australia using units of either square kilometers or square miles depending on which query is used. It uses a "location" scope property to signify the country for which the application is targeted which in turn indicates what units to use. The property is read as scope->location.
One thing to keep in mind is that unless all scopes, including the global one, define the same property, then there is the risk of an attempt to read a property in a scope where it is not defined. Doing so causes a blank value to be returned. A blank matches an empty string and can be updated to a value of any type. calc-square-miles3.taq includes a query in the global scope for which the location property is not defined. In this case, the query completes, as intended, using the default square kilometers unit.
scope australia (location = "Australia")
flow country_area
(
double surface_area = surface_area_Km2,
string units = "km2",
? scope->location == "United States"
query<axiom> au_surface_area_query(surface_area : australia.country_area)
query<axiom> us_surface_area_query(surface_area : usa.country_area)
query<axiom> xx_surface_area_query(surface_area : country_area)
Context Cursor
Am open cursor can be bound to a context list. On change of scope, the cursor is reset and linked to the list belonging to the incoming scope. lists2.taq and lists3.taq both declare context lists of all types in the body of 2 scopes named "london" and "new_york". The latter accesses the lists using an open cursor.
Here is an example of a context list named "fruit", containing either ("strawberry", "cherry", "peach") or ("apple", "pear", "orange"), bound to same-named cursor in a flow which displays each item and the list size
flow fruit
{ cursor item(fruit) }
(
Context Select
foreign-colors.taq has the following select declaration
colors, | Red, | Green, | Blue) |
{ | |||
? aqua: | 0, | 255, | 255 |
? black: | 0, | 0, | 0 |
? blue: | 0, | 0, | 255 |
? white: | 255, | 255, | 255 |
} |
This maps one of four colors to a red-green-blue color value. Each criterion matches a color name to a variable instead of a literal, which indicates there are several names to match on for each selection. The names are provided by a context list. and the list name is inserted as the first name in the select header to create a context select binding.
There is both a German scope and a French one. This is the French "colors" context list
axiom french.colors
( | aqua, | black, | blue, | white ) |
{ | "bleu vert", | "noir", | "bleu", | "blanc"} |
The French solution shows the "shade" in French supplied to each color query and the selected red/green/blue values
color(shade=noir, Red=0, Green=0, Blue=0)
color(shade=blanc, Red=255, Green=255, Blue=255)
color(shade=bleu, Red=0, Green=0, Blue=255)