0

As I understand a thumb rule for allowing expressions in Delphi constants is that the expression should be able to get evaluated in complile time.

So it's greatly unclear for me why the following is not allowed ([dcc64 Error] E2026 Constant expression expected):

const    
  SCAPEGOATALHPA = 2.0 / 3;
  SCAPEGOATCONST = 1.0 / ln(SCAPEGOATALHPA);

Is something that would prevent Delphi compute the natural logarithm of a constant float number? Or just a Delphi bug, as usual?

Zoltán Bíró
  • 346
  • 1
  • 12
  • 1
    The reason is that `Ln` is a normal function, just like one you'd write yourself. Hence it can only be executed at runtime. If you Ctrl-click `Ln`, you will see its source code. The same thing applies to `Sqrt`, for instance. But if you ctrl-click `Rount`, you will not see its source code. That's because that is a "magic" function, and it can be used at compile time. – Andreas Rejbrand May 20 '22 at 12:13
  • In the end you can only combine literals - and `ln()` is a function with a result, not a literal. Don't confuse this with typecasting, which looks the same (i.e. `Double(5)`) and still is a literal. – AmigoJack May 20 '22 at 12:18
  • `Double(5)` isn't a literal, but it is still a constant expression. – Andreas Rejbrand May 20 '22 at 12:19
  • `high` is also accepted: `const XXX = high(integer);` Though `high` is also a normal function, in system unit. So I still cannot see lots of logic in that. – Zoltán Bíró May 20 '22 at 12:25
  • Though you're right, `high` has indeed no source code. – Zoltán Bíró May 20 '22 at 12:27
  • @ZoltánBíró: No, `High` is not a normal function. If you ctrl-click it, you are not taken to its source code. – Andreas Rejbrand May 20 '22 at 12:27
  • Not a bug, just that the language designers didn't choose to support such things – David Heffernan May 20 '22 at 12:51
  • 1
    The docs explicitly list the available functions: Abs, High, Low, Pred, Succ, Chr, Length. Odd, Round, Swap, Hi, Lo, Ord, SizeOf, Trunc: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Declared_Constants#Constant_Expressions – Uwe Raabe May 20 '22 at 12:52

0 Answers0