Sunday, May 20, 2018

Field and constant definition

In this post I will explain how to define fields and constants with Oxygene Pascal.

Field definition
A field is a storage variable within a class or structure, a constant is alike, but immutable.
Local variables and constants within a method implementation work exactly the same (with a few exceptions), so everything that is explained here is also valid for that (the exceptions will pointed out).


A field is defined with the keyword "var", followed by the name of the field and the type of the field:
This is an uninitialized variable, which has the initially the default value for the used type.
A field can be initialized as following:
Both field definitions are the same; at the first I added an explicit type, at the second one the type is inferred from the assigned value - but it is still strong typed.

It is also possible to define multiple fields of the same type at once:
With this syntax it is not possible to initialize the variables to another value than the default of the type.

A field can also be defined as read only:
All three fields are read only, the first to with an assigned value in the field definition, the third must be assigned in the class constructor (the only place where this is allowed for a read only variable) as it has no value in the definition.

Field modifiers
Besides the "readonly" modifier that we already talked about, there are three more:
readonlyThe readonly modifier declare the field as read only, the field can only be initialized within the field definition or in the constructor of the type.
For variables: a read only variable must be initialize within the variable definition.
unsafe
     /
pinned
The unsafe modifier (only for fields) defines that the field is an unsafe pointer - this means that the field may not be moved within memory.
Pinned - only for variables: means that the variable may not be moved within memory
volatileFields only. The volatile modifier makes the field thread safe and all reads and writes to it will be atomic. A volatile field is always accessed directly in memory and won't be cached by the CPU (preventing out of date CPU caches).
implementsFields only. With the implements modifier you tell the compiler that this field points to a method that implements a specific interface method.

Instance and static fields 
(This does not apply to variables)
A field can be defined as an instance field (with var, as I did above in the examples) or as a static field that is the same for all instances with "class var".

Field visibility
(This does not apply to variables)
The visibility of the field is determent by the section of the class where the field is declared, see Visibility modifiers for more information.

Constant definition
A constant is always static and never instance specific.
A constant definition is identical to an initialized variable definition, but is initialized with = instead of := (because this is technically speaking not an assignment) :
The difference between a read only variable and a constant is that a read only variable can be assigned in the constructor and a constant can not. Further, each use of a constant is replaced by the constant itself during compile. So in a compiled binary the constant does not exist anymore.

And that is all I can tell about fields, variables and constants.
Next I will tell about properties, which need both the method and fields.

No comments:

Post a Comment