[Gambas-user] Constant expressions
bb
adamnt42 at gmail.com
Tue Oct 12 20:56:08 CEST 2021
On Tue, 2021-10-12 at 15:00 +0200, Benoît Minisini wrote:
> 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!
>
What! Are you some kind of mind-reader now?
I was looking for exactly that only a few days ago.
8-)
Does it work for Longs as well?
regards
bruce
More information about the User
mailing list