[Gambas-user] Constant expressions

Benoît Minisini g4mba5 at gmail.com
Tue Oct 12 15:00:56 CEST 2021


Hi,

I have implemented in the compiler the ability to use "constant 
expressions".

Constant expressions are integer expressions that are evaluated at 
compile time, and that can be used in the following places:

- Constant declarations.

- Enumeration declarations.

- Embedded array declarations.

These expressions only support a subset of Gambas operators and 
pre-defined constants.

Here is the list of what you can use inside a constant expression:

- Arithmetic operators: +, -, *, /, \, Div, %, Mod

- Logical operators: And, Or, Xor, Not

- Bitwise operators: Shl, Shr, Asl, Asr, Lsl, Lsr

- Size of a native numerical or date datatype: SizeOf()

- Native datatype constants : gb.Boolean, gb.Byte, gb.Short, gb.Integer,
   gb.Long, gb.Single, gb.Float, gb.Date.

- Another integer constant.

WARNING! In the context of constant integer expressions, The / operator 
acts like the integer division operator \.

Note that the bitwise operators now can used like... operators, and must 
be used that way in the context of constant integer expressions

Here are some examples:

   Public Const Alpha As Integer = 1
   Public Const Beta As Integer = (Alpha * 2) + 1
   Public Const Delta As Byte = Beta + Alpha

   Private Enum FirstFlag = 1 Shl 0, SecondFlag = 1 Shl 1,
     ThirdFlag = 1 Shl 2

   Private SomeEmbeddedArray[Delta * SizeOf(gb.Short)] As String

The corresponding documentation is available in the wiki here:

https://gambaswiki.org/edit/lang/constexpr

Enjoy it!

-- 
Benoît Minisini


More information about the User mailing list