4D v16.3

Using tokens in formulas

Home

 
4D v16.3
Using tokens in formulas

Using tokens in formulas  


 

4D's language includes a unique tokenization system for all object names of the language that are used in the code (constants, commands, tables, fields and keywords). Tokenizing these names means that as you type in the code editor they are stored internally as absolute references (numbers) and then restored as text during execution or display depending on the context. This allows you to guarantee that the code will always be interpreted correctly, even if you rename your tables or fields, or when 4D language commands are renamed over the course of different application versions.

Note: This also ensures automatic translation of the code when you have enabled the "Use regional system settings" option on the Methods Page of the Preferences and open your databases with 4D versions in different languages.

Tokenisation is completely transparent for 4D developers when working in the code editor. However, this mechanism is not automatically implemented in 4D formulas since they consist of text that is interpreted at runtime, and not as it is typed. In fact, this is the case as soon as 4D code is expressed as raw text, more specifically when code is exported and then imported using the METHOD GET CODE and METHOD SET CODE commands, copied/pasted or interpreted from 4D HTML Tags.
To continue to benefit from tokenization mechanisms in these contexts, you just need to use an explicit syntax (described below) which consists in preceding object names in the language by their token.

By default, the token mechanism is not implemented automatically in 4D formulas (as well as contexts where 4D code is expressed as raw text, see above). As a result, for named elements contains in expressions, 4D offers a special syntax you can use to reference the tokens directly: you just need to add a specific suffix after the element name to indicate its type (command, field, etc.), followed by its reference. The token syntax is detailed in this table:

ElementExample (standard syntax)SuffixExample (token syntax)Comments
4D CommandString(a):CxxString:C10(a)xx is the command number
Table[Employees]:xx[Employees:1]xx is the table number
Field[Employees]Name:xx[Employees:1]Name:2xx is the field number
4D PluginPV PRINT(area):Pxx:yyPV PRINT:P13000:229(area)xx is the plug-in ID and yy is the index of the command

Note: Uppercase letters (C, P) must be used in the suffixes; otherwise, they will not be interpreted correctly.

When you use this syntax, you guarantee that your formulas will be interpreted correctly even in the case of renaming or when the database is executed in a different language.

Note : Constants are also tokenized in the language however in formulas you can just pass their value in order make them independent of the context. 

This syntax is accepted in all 4D formulas (or 4D expressions) regardless of the calling context:

The token syntax requires the addition of the reference numbers of various elements. The location of these references depends on the type of element.

Command numbers can be found in this Language Reference manual ("Properties" area) as well as on the Commands page of the Explorer:

Table and field numbers can be obtained using the Table and Field commands.

They are also displayed in the Inspector palette of the Structure editor:

To know what the tokens are for 4D plug-in commands, the trick is to enter the desired code in the Method editor and then restart 4D after disabling the plug-in (for example, by moving its folder). This means that only tokens will be displayed in the Method editor, and you can then copy the ones you need.

Code with plug-in installed:

The same code after plug-in has been disabled:



See also 

METHOD GET CODE

 
PROPERTIES 

Product: 4D
Theme: Formulas

 
HISTORY 

 
ARTICLE USAGE

4D Language Reference ( 4D v16)
4D Language Reference ( 4D v16.1)
4D Language Reference ( 4D v16.2)
4D Language Reference ( 4D v16.3)