4D v16.3

Introduction to the 4D Language

Home

 
4D v16.3
Introduction to the 4D Language

Introduction to the 4D Language  


 

 

The 4D language is made up of various components that help you perform tasks and manage your data.

  • Data types: Classifications of data in a database. See discussion in this section as well as the detailed discussion in the section Data Types.
  • Variables: Temporary storage places for data in memory. See detailed discussion in the section Variables.
  • Operators: Symbols that perform a calculation between two values. See discussion in this section as well as the detailed discussion in the section Operators and its subsections.
  • Expressions: Combinations of other components that result in a value. See discussion in this section.
  • Commands: Built-in instructions to perform an action. All 4D commands, such as ADD RECORD, are described in this manual, grouped by theme; when necessary, the theme is preceded by an introductory section. You can use 4D Plug-ins to add new commands to your 4D development environment. For example, once you have added the 4D Write Plug-in to your 4D system, the 4D Write commands become available for creating and manipulating word-processing documents.
  • Predefined constants: Constant values accessible by name. For example, XML DATA is a constant (value 6). Predefined constants allow writing more readable code. Constants are described with the commands that use them, and are fully listed in the List of constant themes section.
  • Methods: Instructions that you write using all parts of the language listed here. See discussion in the section Methods and its subsections.

This section introduces Data Types, Operators, and Expressions. For the other components, refer to the sections cited above.

In addition:

  • Language components, such as variables, have names called Identifiers. For a detailed discussion about identifiers and the rules for naming objects, refer to the section Identifiers.
  • To learn more about array variables, refer to the section Arrays.
  • If you plan to compile your database, refer to the section Compiler Commands as well as the Design Reference manual of 4D.

Starting with 4D v15, 4D's Method editor uses the international "English-US" language by default, regardless of the 4D version or local system settings. This feature neutralizes any regional variations that might disrupt code interpretation between 4D applications (date formats for instance); and in French versions of 4D, commands and constants are now written in "English-US" as is already the case in other languages.

This default setting provides 4D developers with several advantages:

  • It facilitates code sharing between developers, regardless of their country, regional settings, or the 4D version used. A 4D method can now be exchanged by simple copy/paste, or saved in a text file, with no compatibility issues.
  • It also makes it possible to include 4D methods in source control tools, which often require exports to be independent from regional settings and languages.

This setting can be disabled using the "Use regional system settings" option in the 4D Preferences dialog box (see the Methods Page).

The English-US settings may have several effects on the way you write methods. This concerns code written in development mode as well as formulas in deployed applications. In this mode, code must comply with following rules:

  • Decimal separators for real numbers must now be periods (".") in all versions (and not commas (",") as is the custom in French, for example).
  • Date constants must now use the ISO format (!YYYY-MM-DD!) in all versions.
  • Command and constant names must be in English (this change only concerns French versions of 4D, since this was already the case with other languages).

Note: The Method editor includes specific mechanisms that automatically fix incorrect entries if necessary.

The following table illustrates differences between code in 4D v15 (or higher) and in previous versions:

Code sample in methods/formulas
4D v15 and higher (default mode, all versions)a:=12.50
b:=!2013-12-31!
Current date
4D v14 or 4D v15 (preference checked, US version, for instance)a:=12.50
b:=!12/31/2013!
Current date
4D v14 or 4D v15 (preference checked, French version)a:=12,50
b:=!31/12/2013!
Date du jour

Note: When the preference is checked, real and date formats are based on system settings.

In the language, the various types of data that can be stored in a 4D database are referred to as data types. There are eight basic data types: string, numeric, date, time, Boolean, picture, and pointer.

  • String: A series of characters, such as “Hello there”. Text fields and variables, as well as Alpha fields, are of the String data type
  • Numeric: Numbers, such as 2 or 1,000.67. Integer, Long Integer, and Real fields and variables are of the numeric data type.
  • Date: Calendar dates, such as 1/20/89. Date fields and variables are of the date data type.
  • Time: Times, including hours, minutes, and seconds, such as 1:00:00 or 4:35:30 PM. Time fields and variables are of the time data type.
  • Boolean: Logical values of TRUE or FALSE. Boolean fields and variables are of the Boolean data type.
  • Picture: Picture fields and variables are of the picture data type.
  • Pointer: A special type of data used in advanced programming. Pointer variables are of the pointer data type. There is no corresponding field type.
  • Object: Composite data type which can contain any type of data sets in the form of key/value pairs. Object fields and variables are of the Object data type.

Note that in the list of data types, the string and numeric data types are associated with more than one type of field. When data is put into a field, the language automatically converts the data to the correct type for the field. For example, if an integer field is used, its data is automatically treated as numeric. In other words, you need not worry about mixing similar field types when using the language; it will manage them for you.

However, when using the language it is important that you do not mix different data types. In the same way that it makes no sense to store “ABC” in a Date field, it makes no sense to put “ABC” in a variable used for dates. In most cases, 4D is very tolerant and will try to make sense of what you are doing. For example, if you add a number to a date, 4D will assume that you want to add that number of days to the date, but if you try to add a string to a date, 4D will tell you that the operation cannot work.

There are cases in which you need to store data as one type and use it as another type. The language contains a full complement of commands that let you convert from one data type to another. For example, you may need to create a part number that starts with a number and ends with characters such as “abc”. In this case, you might write:

 [Products]Part Number:=String(Number)+"abc"

If Number is 17, then [Products]Part Number will get the string “17abc”.

The data types are fully defined in the section Data Types.

When you use the language, it is rare that you will simply want a piece of data. It is more likely that you will want to do something to or with that data. You perform such calculations with operators. Operators, in general, take two pieces of data and perform an operation on them that results in a new piece of data. You are already familiar with many operators. For example, 1 + 2 uses the addition (or plus sign) operator to add two numbers together, and the result is 3. This table shows some familiar numeric operators:

OperatorOperationExample
+Addition1 + 2 results in 3
Subtraction3 – 2 results in 1
*Multiplication2 * 3 results in 6
/Division6 / 2 results in 3

Numeric operators are just one type of operator available to you. 4D supports many different types of data, such as numbers, text, dates, and pictures, so there are operators that perform operations on these different data types.

The same symbols are often used for different operations, depending on the data type. For example, the plus sign (+) performs different operations with different data:

Data TypeOperationExample
NumberAddition1 + 2 adds the numbers and results in 3
StringConcatenation“Hello ” + “there” concatenates (joins together)
the strings and results in “Hello there”
Date and NumberDate addition!1989-01-01! + 20 adds 20 days to the date
January 1, 1989, and results in the date
January 21, 1989

The operators are fully defined in the chapter Operators and its subsections.

Simply put, expressions return a value. In fact, when using the 4D language, you use expressions all the time and tend to think of them only in terms of the value they represent. Expressions are also sometimes referred to as formulas.

Expressions are made up of almost all the other parts of the language: commands, operators, variables, and fields. You use expressions to build statements (lines of code), which in turn are used to build methods. The language uses expressions wherever it needs a piece of data.

Expressions rarely “stand alone.” There are only a few places in 4D where an expression can be used by itself:

  • Query by Formula dialog box
  • Debugger where the value of expressions can be checked
  • Apply Formula dialog box
  • Quick Report editor as a formula for a column

An expression can simply be a constant, such as the number 4 or the string “Hello.” As the name implies, a constant’s value never changes. It is when operators are introduced that expressions start to get interesting. In preceding sections you have already seen expressions that use operators. For example, 4 + 2 is an expression that uses the addition operator to add two numbers together and return the result 6.

You refer to an expression by the data type it returns. There are eight expression types:

  • String expression
  • Numeric expression (also referred to as number)
  • Date expression
  • Time expression
  • Boolean expression
  • Picture expression
  • Pointer expression
  • Object expression.

The following table gives examples of each type of expression.

ExpressionTypeExplanation
“Hello”StringThe word Hello is a string constant,
indicated by the double quotation marks.
“Hello ” + “there”StringTwo strings, “Hello ” and “there”,
are added together (concatenated)
with the string concatenation operator (+).
The string “Hello there” is returned.
“Mr. ” + [People]NameStringTwo strings are concatenated:
the string “Mr. ” and the current value
of the Name field in the People table.
If the field contains “Smith”, the expression
returns “Mr. Smith”.
Uppercase(“smith”)StringThis expression uses Uppercase,
a command from the language,
to convert the string “smith” to uppercase.
It returns “SMITH”.
4Number This is a number constant, 4.
4 * 2NumberTwo numbers, 4 and 2, are multiplied
using the multiplication operator (*).
The result is the number 8.
My ButtonNumberThis is the name of a button.
It returns the current value of the button:
1 if it was clicked, 0 if not.
!1997-01-25!DateThis is a date constant for the date 1/25/97
(January 25, 1997).
Current date + 30DateThis is a date expression that uses
the Current date command to get today’s date.
It adds 30 days to today’s date and returns
the new date.
?8:05:30?TimeThis is a time constant that represents 8 hours,
5 minutes, and 30 seconds.
?2:03:04? + ?1:02:03?TimeThis expression adds two times together and
returns the time 3:05:07.
TrueBooleanThis command returns the Boolean value TRUE.
10 # 20BooleanThis is a logical comparison between two numbers.
The number sign (#) means “is not equal to”.
Since 10 “is not equal to” 20, the expression
returns TRUE.
“ABC” = “XYZ”BooleanThis is a logical comparison between two strings.
They are not equal, so the expression returns FALSE.
My Picture + 50PictureThis expression takes the picture in My Picture,
moves it 50 pixels to the right, and returns
the resulting picture.
->[People]NamePointerThis expression returns a pointer to the field
called [People]Name.
Table (1)PointerThis is a command that returns a pointer to
the first table.
JSON Parse(MyString)ObjectThis is a command that returns MyString as an object (if proper format)



See also 

Arrays
Constants
Data Types
Methods
Operators
Pointers
Variables

 
PROPERTIES 

Product: 4D
Theme: Language definition

 
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)