Enhancing Compilation with Direct Typing in 4D

With 4D 20 R7 you can say goodbye to “Compiler_” methods and embrace more freedom in how and where you declare your variables. Save time by staying focus on your development workflow and your coding process thanks to Direct Typing.

In earlier versions of 4D, developers had two options for handling variable typing during compilation. If the “Compilation path” setting was set to anything other than “All variables are typed,” the compiler would attempt to infer the types of untyped variables based on their usage in the code. While this could simplify variable declarations, it came with a trade-off:

  • Slower Compilation: Typing inference requires an additional compilation pass, slowing down the overall build process.
  • Risk of Errors: The compiler might infer incorrect types, potentially introducing difficult debugging bugs.

 

To avoid these issues, developers were advised to enforce strict typing by using the “All variables are typed” option. However, this required declaring all process and inter-process variables within dedicated “Compiler_” methods.

Introducing Direct Typing

To address these challenges, we’ve introduced Direct Typing which simplifies the way variables are declared and typed during compilation.

With Direct Typing, you no longer need to use “Compiler_” methods to declare process or inter-process variables. Instead, you can now define them wherever they logically belong in your code, whether that’s within a method, a class, or any other appropriate context. This gives you greater control over your code’s organization and enhances encapsulation.

This change provides the flexibility to organize your code the way you want, while still ensuring type safety and efficient compilation.

Adopting Direct Typing in Your Projects

Whether you’re working on a new or an existing project, Direct Typing can be easily integrated into your workflow.

  • New projects always use Direct Typing. This means you can start enjoying the benefits of flexible variable declaration and better code organization right away.

  • For existing projects, you’ll find a new setting in the Compilation Path that allows you to toggle Direct Typing on or off. This ensures compatibility with legacy code, giving you the option to migrate at your own pace.

Simply enable this setting, and you’ll be free to declare your process and inter-process variables wherever it makes the most sense in your code.

What’s new with direct Typing?

The #declare syntax is now mandatory for declaring method parameters with direct typing, rendering traditional compiler commands like C_TEXT, C_LONGINT, and others obsolete. One of the key benefits is that, particularly in components, you no longer need to compile the components when compiling a host base. The compiler will automatically fetch the method prototypes directly from the component’s code, simplifying and speeding up the development process.

To assist in converting compiler commands like C_TEXT and C_LONGINT to the var declaration syntax, we’ve made a 4D macro available on GitHub. You can access it here: 4D-Macros on GitHub.

how does it work?

  • When the compiler encounters a variable that hasn’t been explicitly typed, it adds that variable to a list of “to be typed” variables.
  • The compiler then temporarily pauses the compilation of the current method and proceeds to compile other methods.
  • Before pausing, it collects all the variable definitions in the method.
  • After compiling all the methods, the compiler checks if all the variables on the “to be typed” list have been found and assigned a type.
    • If so, it resumes compiling the previously paused methods.
    • If not, errors will be generated to indicate untyped variables.

Conclusion

The introduction of Direct Typing marks a significant improvement in how variable declarations are handled in 4D. By eliminating the need for “Compiler_” methods and allowing variable declarations to be made freely within the code, 4D developers now have greater flexibility and control over how they structure their projects.

To learn more about Direct Typing and how to implement it in your projects, check out our documentation.

Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.