PiedSoftware 3 Posted March 25 (edited) type TCodeStructure = record StartTkn: string; EndTkn: string; SubTkns: array of string; end; type TIfTkns = (itElse, itElsif); const elseTkn = '#else'; elsifTkn = '#elseif'; IfTkns: array[TIfTkns] of string = (elseTkn, elsifTkn); IfStruct: TCodeStructure = (StartTkn: '#if'; EndTkn: '#endif'; SubTkns: [IfTkns[itElse], IfTkns[itElsif]]); I wrote this code and got the error "E2026 Constant expression expected" on the last line. I also tried SubTkns: IfTkns); When I changed it to SubTkns: [elseTkn, elsifTkn]); it compiled happily. and that gave me E2010 Incompatible types: 'Dynamic array' and 'Array'. To me it seems that Delphi should allow the first thing I tried - referencing a const array via constant indices to specify a constant. There is no reason to disallow that sort of thing. Ideally even the second attempt should be reasonable. I have Delphi 10.4. Edited March 25 by PiedSoftware Share this post Link to post
PeterBelow 238 Posted March 25 A typed constant (that's what your IfTkns constant is) is actually implemented as a compiler-initialized variable, and that's the reason you cannot use it as a constant expression. It may be counter-intuitive but that's the way it works and you have to live with it. Share this post Link to post
David Heffernan 2345 Posted March 25 There's all sorts of things that Delphi should allow as constants, but that's a long known area of significant weakness in the language. Share this post Link to post
PiedSoftware 3 Posted March 26 Yes, David, I agree. I would like them to fill that out: everything that is logically able to be evaluated at compile time should be assignable to a constant. Share this post Link to post
David Heffernan 2345 Posted March 26 7 hours ago, PiedSoftware said: Yes, David, I agree. I would like them to fill that out: everything that is logically able to be evaluated at compile time should be assignable to a constant. We'd all like that. Prospects are not good for it happening. Share this post Link to post
PiedSoftware 3 Posted April 1 (edited) I guess I am a bit spoiled playing with Swift. It lets you assign any expression to a constant, compile time or run time. Edited April 1 by PiedSoftware 1 Share this post Link to post
Ian Barker 19 Posted April 5 On 4/1/2024 at 12:54 AM, PiedSoftware said: expression to a constant, compile time or run time A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood? Share this post Link to post
DelphiUdIT 176 Posted April 5 3 minutes ago, Ian Barker said: A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood? Constant when you write the code, variable when the code is running Share this post Link to post
pmcgee 10 Posted April 5 (edited) That wouldn't be strange in C++ ... int i = 42; int main() { const int j = i; const int* p = &j; } Edited April 6 by pmcgee Share this post Link to post
Stefan Glienke 2002 Posted April 5 1 hour ago, Ian Barker said: A constant you can assign at run time? Is that a one-off thing - assign once and then it's immutable? If not, then it's a variable, not a constant or have I misunderstood? Imagine you could write this code in Delphi: var i: Integer = 42; begin const j = i; Writeln(j); end. Oh, wait, you can. Too bad that the syntax is not really clean as it mimics the const declaration using the equal sign while it really is what other languages call immutable variable and thus should have used the assign operator but people with even less expertise in programming language design than me disagreed. 2 Share this post Link to post
DelphiUdIT 176 Posted April 5 (edited) In Delphi is allowed to change typed const, look here ... simple, if the relative option is set. Never used ... is terrible... Edited April 5 by DelphiUdIT Share this post Link to post
Brandon Staggs 278 Posted April 5 43 minutes ago, DelphiUdIT said: In Delphi is allowed to change typed const, look here ... simple, if the relative option is set. Never used ... is terrible... https://stackoverflow.com/a/49060/2501336 Share this post Link to post
DelphiUdIT 176 Posted April 5 22 minutes ago, Brandon Staggs said: https://stackoverflow.com/a/49060/2501336 Oh yes, I remember something about Delphi7 ... but now these things should never be present, neither like options ... a code refactoring is a must. We are talking in these days about safe memory .... and there are still those "options". But this is my personal opinion. Share this post Link to post
PiedSoftware 3 Posted April 8 The way Swift works is that you can declare value with either the keyword let or the keyword var. If you use let the value cannot be changed. But the expression you assign to a let value can be anything available at that point. 1 Share this post Link to post