So. By now we know what lives in the Global Context and how we can use it in our source code. Let’s see what else our Script Engine is capable of, and what kind of syntax it understands.
Let me start with the primitive types and operations we can perform on each of them.
Null is used only in the Query Language, so let’s just skip it for now.
The Undefined type has to do with some rather important concepts, so let’s pause here for a while.
The thing is that 1C:Enterprise supports both implicit and explicit variable declarations. So, you don’t have to, but you can declare variables explicitly just like this.
What 1C does not support is the static typing, meaning that you cannot tell the Platform what type of a variable you want this to be.
So what is the type of this variable then? Let’s see. I’m at the breakpoint, and here is my variable value in the debugger. Its type is Undefined. I mean, it’s well defined, it’s just how this type is called.
This type allows the single literal value that’s also called Undefined and gets assigned automatically whenever the variable of this type is created. So, if I ask the debugger to check this condition, True is the answer.
We use this condition quite a lot, mostly to check if the parameter is passed to the method. When we declare the parameter like this, it means that it can be skipped by the calling procedure, in which case the Undefined value will be assigned to it. Then we can check this value and fork the execution depending on the result.
Another example of using the Undefined literal of the Undefined type is when we call a method that calculates some of its parameters for us.
We cannot call the procedure without declaring all the parameters first. This is what I get if I try. But I have nothing to fill them out with. This is what I want the procedure to do. So I just declare them like this. Or like this, which is exactly the same thing. And there are no errors anymore.
So, this was the Undefined type and its one and only literal.
Next up: the Number type.
There is nothing too fancy or unusual about the Number type - all too familiar staff you already know if you’ve ever written any code.
You can use literals containing up to 38 digits and a decimal point. And you can perform all sorts of math operations on your numbers. You can use brackets to change the standard operations order. And, of course, you can compare numbers like this.
As easy as that.
Next one: the String type
String literals have to be double quoted. Please note that the single quotation marks won’t do for they are used for the date literals only.
What you do if you need a double quotation mark inside of the string? Easy. You just double it down. This is how it will look like at the runtime.
Sometimes you need to specify a multiline string. The most common way of doing this in 1C:Enterprise is using the pipe symbol like this. And this is the string at the runtime. We usually use this pipe notation when writing queries like here for example.
And this is what we do when we need to combine, or as we programmers say, concatenate several strings: we just add them to one another like this. And this is the result at the runtime.
We also can compare strings like this, which works according to the alphabetical sort order.
This was our Sting type.
Next up: the Date type.
To specify a date literal, we use this format. Please note that the literal is in the single quotation marks, not the double ones.
At the runtime this variable will get the Date type and here is its value. Literals of this type ignore anything but digits, so you can add any other symbols as separators and it will work just fine.
Internally, the Platform represents the Date as a number of seconds elapsed since 12 AM, Jan 01, year 1 of the Common Era. It means that we can easily get away with things like this (which gives us the number of seconds between these two dates) or even this (which gives us the same time on the day before).
And of course, we can compare dates just like this.
OK. Moving on. Boolean type.
Two literals - True and False - are used in the source code "as is," without any quotation marks or something.
Values of this type are results of logical expressions, so we can use them in conditions.
Or we can assign this result to a variable like so. And this is how it looks like at the runtime.
OK. And the last one. The Type type.
I mean, the type that’s called Type, however weird it may sound. Anyway.
The thing is that, as we already saw, we cannot define the variable type explicitly. The variable gets typed as soon as a value (that always has some specific type) is assigned to it.
One can even say that variables in 1C:Enterprise have no types, only values do.
I can put anything in here, so I need a way of telling what the type of its current value is.
Here is how we do this. If we get back to the Global Context, we’ll see these Type and TypeOf functions, both returning the Type-typed value.
And this is how they work. I can ask the TypeOf function what type this variable has, and this is what I will see.
As for the Type function, it returns the Type given the type’s name like this. Why do we need it?
This is why. The thing is, that there are no literals of the Type type, so we use this Type function with the name of the type to compare another type with it.
And of course we can use not only primitive type names here, but any other type known to the Platform, including the Metadata-Objects-related types.
So, these were our primitive types.