Tommi Prami 130 Posted September 26 Helllo, Defining constants is pretty limited in delphi. some I think, related to single pass compiler. Latest was this case // Works MULTIPLIER = 1000; MULTIPLIED_CONSTANT = 1E-11 * MULTIPLIER; OTHER_FLOAT_CONSTANT: Double = 0.005; // Don't work MULTIPLIER: NativeInt = 1000; MULTIPLIED_CONSTANT = 1E-11 * MULTIPLIER; // <- E2026 Constant expression expected This seems kind of weird. Is this limitation or bug in the Compiler, or feature I don't know about. -Tee- Share this post Link to post
Virgo 18 Posted September 26 36 minutes ago, Tommi Prami said: Is this limitation or bug in the Compiler, or feature I don't know about. From documentation " Typed constants, unlike true constants, can hold values of array, record, procedural, and pointer types. Typed constants cannot occur in constant expressions. ". Try MULTIPLIER = NativeInt(1000); Share this post Link to post
uligerhardt 18 Posted September 26 Only untyped constants are "real" constants. Typed constants are more like variables you can't change. That's just how Delphi works. Share this post Link to post
Uwe Raabe 2059 Posted September 26 2 hours ago, uligerhardt said: Typed constants are more like variables you can't change. ... unless you allow that with a compiler directive: Writeable typed constants 3 Share this post Link to post
mvanrijnen 123 Posted September 26 4 hours ago, uligerhardt said: Only untyped constants are "real" constants. Typed constants are more like variables you can't change. That's just how Delphi works. yes, so called "CONSTANT VARIABLES" 😉 1 Share this post Link to post
Cristian Peța 103 Posted September 26 (edited) Compiler error var foo: Integer = 10; //E2195 Cannot initialize local variables begin foo := 20; This compiles {$WRITEABLECONST ON} const foo: Integer = 10; begin foo := 20; Edited September 26 by Cristian Peța Share this post Link to post
Virgo 18 Posted September 26 54 minutes ago, Cristian Peța said: This compiles {$WRITEABLECONST ON} const foo: Integer = 10; begin foo := 20; But at next invocation of such function foo is already 20. So typed const are like static variables in that sense. Also, in past Delphi versions they were writable by default (Turbo Pascal compatibility). Share this post Link to post
Lars Fosdal 1792 Posted September 26 How about an untyped Multiplier and MULTIPLIED_CONSTANT = 1E-11 * NativeInt(MULTIPLIER); ? 1 Share this post Link to post
Stefan Glienke 2009 Posted September 26 1 hour ago, Cristian Peța said: Compiler error var foo: Integer = 10; //E2195 Cannot initialize local variables begin foo := 20; This compiles {$WRITEABLECONST ON} const foo: Integer = 10; begin foo := 20; Clever way of introducing thread-unsafety 😉 1 1 Share this post Link to post
Uwe Raabe 2059 Posted September 26 4 minutes ago, Stefan Glienke said: Clever way of introducing thread-unsafety I wonder when anyone comes up with a request for threadconst. 3 1 Share this post Link to post
Remy Lebeau 1403 Posted September 26 3 hours ago, Cristian Peța said: Compiler error var foo: Integer = 10; //E2195 Cannot initialize local variables begin foo := 20; It is true that variables can't be initialized when using the 'var' block at the top of the function. But inline variables can be initialized: begin var foo: Integer := 10; // OK 1 Share this post Link to post
Tommi Prami 130 Posted September 27 Did not consider thsat they are those hybrid "varconsts", if type defined. If type could be defined for const could make some code cleaner. Make sure, some constant is always Double, not extended, in 32bit and also in 64bit... But anyways, thanks for info y'all... -tee- Share this post Link to post
uligerhardt 18 Posted September 27 (edited) 3 hours ago, Tommi Prami said: If type could be defined for const could make some code cleaner. Make sure, some constant is always Double, not extended, in 32bit and also in 64bit... You can use cast syntax - not sure how much that helps: const OTHER_FLOAT_CONSTANT Double0.005); Edited September 27 by uligerhardt Share this post Link to post
Remy Lebeau 1403 Posted September 27 7 hours ago, uligerhardt said: You can use cast syntax - not sure how much that helps: const OTHER_FLOAT_CONSTANT Double0.005); const OTHER_FLOAT_CONSTANT = Double(0.005); Share this post Link to post
David Heffernan 2347 Posted September 27 2 hours ago, Remy Lebeau said: const OTHER_FLOAT_CONSTANT = Double(0.005); What problem does this solve? Share this post Link to post
Remy Lebeau 1403 Posted September 27 9 minutes ago, David Heffernan said: What problem does this solve? I was simply fixing the broken syntax in uligerhardt's reply. Share this post Link to post
David Heffernan 2347 Posted September 27 8 minutes ago, Remy Lebeau said: I was simply fixing the broken syntax in uligerhardt's reply. Does it even compile, and if so what does it mean? Share this post Link to post
uligerhardt 18 Posted September 28 17 hours ago, Remy Lebeau said: const OTHER_FLOAT_CONSTANT = Double(0.005); Hmm, my code got garbled somehow. Thanks for the correction. Share this post Link to post
uligerhardt 18 Posted September 28 14 hours ago, David Heffernan said: Does it even compile, and if so what does it mean? My fragment wouldn't have compiled. And as stated I'm not sure if it helps with this: On 9/27/2024 at 6:16 AM, Tommi Prami said: Make sure, some constant is always Double, not extended, in 32bit and also in 64bit... That might depend on the scenario. IIRC this syntax is used in the RTL/VCL code - can't check right now. Share this post Link to post