Up to Main Index Up to Journal for November, 2023 JOURNAL FOR SUNDAY 12TH NOVEMBER, 2023 ______________________________________________________________________________ SUBJECT: On adding constants DATE: Sun 12 Nov 19:48:35 GMT 2023 The work on adding the new data type any I believe is now complete. I need to finish final testing and documentation before publishing a release. However, I recently had my Covid and flu shots and feel awful :( In the meantime I’ve been idly toying away at a feature often requested, constant values. Constants are just that, variables you set to a value once and that value never changes again for the execution of the program. The syntax implemented so far looks like this: const i := 3 Here the variable i is set to the integer value 3 and it cannot be changed. Only the inferred define and assign ‘:=’ form of variable declaration is supported by the const operator for now. A constant can be initialised to any expression. For example: const i := 3 + 5 const config := call init Arrays and maps may be constants. However, while a different array or map cannot be assigned to the constants the elements can be modified. This may change in the future as I believe a constant should be completely constant. Constants may be defined in functions: // circle returns the area of the circle for the given radius circle: func radius:float const π := 245850922.0 / 78256779.0 // π is 3.141592653589793 return π * radius * radius endfunc println call circle 2.0 // displays 12.566370614359172 println call circle 4.0 // displays 50.26548245743669 One issue I’m still working on, functions do not have visibility of global constants yet. I did have some optimisations for constants working. The compiler should be smart enough to replace constant variables with their literal values. For example, given: const i := 3 println i In this case the compiler should rewrite ‘println i’ as ‘println 3’, omitting the variable access. I’m now on my third attempt at implementing constants and the optimisations for this iteration have not been added yet. It should be noted that such optimisations would only apply for constants of simple types that are not conditionally defined. What happened to the other two attempts? They were… very messy — mostly due to my sick and addled brain at the moment. This third implementation is clean and simple with only a few code changes. -- Diddymus Up to Main Index Up to Journal for November, 2023