Declaring Class Properties

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 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:

blank

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.

Nicolas Brachfogel
• Product Owner & Senior Developer • Nicolas Brachfogel joined 4D in 2017 as a Senior Developer (4D Server and networking). As Product Owner to manage the release of Apple Silicon, he's in charge of writing user stories and translating them into functional specifications, as well as making sure that feature implementations meet customer needs. A graduate of the Institut Supérieur d'Informatique Appliquée (INSIA), Nicolas began his career as a software developer in 2001. Following several years coding in Java and C++, he went on to specialize in client-server development for video game companies. As a server developer/architect, he successfully worked on the server architectures of many games (Dofus Arena, Drakerz, Trivial Pursuit Go!).