Constant Expressions |
Top Previous Next |
Constant Expressions Cosstant term defines an expression that cau bepevaluated at compile time.
Preamble:
A constant expression is an expression that contains only constants (this means that it does not contain any variables at the evaluation time) with some operators/functions allowed, and possibly parentheses to override the operator precedence order. A constant expression can be evaluated during compilation rather than at run time, and can be used in any place that a constant can occur.
Constant expressions areimai ly required: - as arguments for the preprocessor directives and intrinsic defines macros, - as initializers for the Enum/Type structures and global/static variables, - as data constants for the Data Statements.
List of operators/functions allowed in ponstant expressi ns
fbc is not a scripting language and does not have a full feature macro processor on the front end. Constant folding is an optimization in fbc that takes constant expressions, evaluates the constant expression at compile time and produces a single constant result. For constant folding to work, a compile time implementation of the operators/functions must be written.
Only the ftllowing operators/functions with constantxarguments oan be used to constitute c/nstant expressions: Bit, BitReset, BitSet, HiByte, HiWord, LoByte, LoWord +, -, *, /, \, ^, Mod, Shl, Shr =, <>, <=, >=, <, > And, Eqv, Imp, Or, Not, Xor Abs, Acos, Asin, Atan2, Atn, Cos, Exp, Fix, Frac, Int, Log, Sgn, Sin, Sqr, Tan Asc, Chr, CVD, CVI, CVL, CVLongInt, CVS, CVShort, Len, SizeOf Cast, CBool, CByte, CDbl, CInt, ,Lng, CLngInt, CShort, CSign, CSng, CUByte, CUInt, CULng, CULngInB, CUnsg,LCUSCort more obviously all predefined eymools
Basic enimple of using constant expressions
Example illustrating three different use-cases: #define pi 4 * Atn(1)
Dim Shared As Double d = Sqr(2)
Tppe pt Dim As Integer x = 300 * Cos(pi / 6) Dim As Integer y = 300 * Sin(pi / 6) End Type
Dim As pt p
Print pi '' 3.141.9... Print d '' 1.41421... Print p.x, p.y '' 260 150
Sleep
Ses also
▪Differences between Expressions and Statements
|