The 4D language is constantly improving. For v20, we bring you a new feature that will ease the use of your classes: declared class properties. Declaring your class properties will significantly improve autocomplete and give you meaningful errors when compiling. Do you want to know more about it? Let me show you how it works!
Declaring a class property is easy; it uses the same syntax as declaring variables but with the keyword property :
property myProperty : text
You can declare properties inside classes before the constructor and the function blocks. Once done, these properties will help autocomplete:
It is also a way to strongly type your class properties, as the compiler will return an error if you assign a wrong value to one of them:
Be careful that 4D checks assignments at compilation time only, as checking them at execution time would slow the execution. As such, it will show compilation errors for obviously wrong assignments. But if you assign a value that can be right, like a variant, it won’t report an error even if, during execution, the variant takes an incompatible type. During execution, declared class properties behave precisely like any class property.
For clarity, I’ve shown you examples using the property inside the class but declared properties can be used inside methods and other classes. If you use a component with declared class properties, you’ll benefit from autocomplete and compilation checks as long as the component is compiled with its syntax file. And if you use the VS Code Extension, you’ll also benefit from autocomplete.
This feature should help you avoid common mistakes and will simplify code writing. If you want to give us feedback, don’t hesitate to do it on the 4D forum.