A disagreement with the Udemy Lazarus instructor

In taking this course the instructor describes using “Typed constants” to pre-initialize arrays. These “constants” can later be changed. Well doesn’t that nullify the meaning of constants? So I took his example and moved the pre-initalized array out of the const section and moved it to the var section. As far as I could tell the results were exactly the same. Except now, if you later change the array it makes sense that it is allowed.

I think he got a little aggravated in me trying to make my point. Because at the end he say’s “Frankly, use whichever you want”. But I just couldn’t see why you would pre-initialize an array in the const section (which implies that you can’t change it later… but you can!) if you could do the same thing in the var section (which implies that you can change it later).

He went on to tell me to read page 63 of his book, where he say The way to do this… Not A way to do this!

He later said… “I would generally recommend declaring variables first and then initializing, and using typed consts when you need to pre-initialize“.

But as I show below you don’t need typed consts to pre-initialize an array.

This is what the official documentation for version 3.2.2 (the current version used with the current Lazarus version 3), says about Typed constants.

“Contrary to ordinary constants, a value can be assigned to them at run-time. This is an old concept from Turbo Pascal, WHICH HAS BEEN REPLACED WITH SUPPORT FOR INITIALIZED VARIABLES

Free Pascal itself says, Typed constants is an “is an old concept from Turbo Pascal” and has been replaced! here is an actual screenshot.


However as seen below you don’t need to use typed consts!

Below is his example… pre-initialize an array in the const section. As you can see he changed the 2nd array entry.

In this example I moved the the pre-initialized array to the var section. The results are the same.

Furthermore Free Pascal provides a compiler switch, called “$J or $WRITEABLECONST”. You can use it if you want a constant to act like a constant! As you can see below, with this switch off the compiler doesn’t allow you to change a constant.

My whole issue with the instructor was his attitude of Typed constants being The Way, to initialize arrays!

He also ended by saying “Typed constants have become widely used in Delphi, however, so when absolutely required I would occasionally use those”. However Embarcadero (the owners of Delphi) say “In early versions of Delphi and Object Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.

Sorry, he’s probably forgotten more Pascal and Lazarus than I know. But in this case he’s wrong., I didn’t take the course to learn Pascal and Lazarus because I already was somewhat familiar with it, from teaching myself it, in the early and mid 2000s. I took the course for the OOP aspects which I’m admittedly weak in. I certainly don’t regret taking the course. And I would recommend it for people wanting to learn it.

Also it gets me coding in it again. I’m constantly forgetting the semicolon.

Sorry to beat a dead horse but this is what Wikipedia says…
const (computer programming)

In some programming languages, const is a type qualifier (a keyword applied to a data type) that indicates that the data is read-only.

Well it’s certainly not read only if you can change it!

It just makes no sense to initialize something in a const section… that you can later change!