I would like to suggest adding support for persistent read-only references or pointers in CODESYS, similar to a C++ pointer-to-const (const T*).
For example, something conceptually similar to:
POINTER TO CONSTANT ST_MyData
or
REFERENCE TO CONSTANT ST_MyData
The important distinction is that the reference itself may still be passed around or reassigned, but the referenced object cannot be modified through that reference.
CODESYS currently supports VAR_IN_OUT CONSTANT, which provides read-only reference semantics for parameters, but there does not appear to be an equivalent type that can be stored in a variable or returned from a method.
A practical use case is a registry that owns long-lived definitions.
The registry needs to retain ownership of the data while downstream systems maintain persistent references to it.
Currently, the main options are:
Return the struct by value, which loses reference semantics and requires a copy.
Return POINTER TO ST_Definition, which allows consumers to modify registry-owned data.
Convert the structure into a Function Block and expose a getter-only interface, which provides the desired encapsulation but introduces more complexity and memory overhead.
A read-only reference/pointer type would allow libraries to expose persistent, zero-copy access to internally owned data while preserving encapsulation.
This would be particularly useful for library APIs and object-oriented CODESYS architectures where ownership of data should remain with one component while other components are allowed to observe it.
Similar requests for constant references/pointers have been discussed on CODESYS Forge previously, so this appears to be a recurring use case rather than something specific to one application.
π
3
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I would like to suggest adding support for persistent read-only references or pointers in CODESYS, similar to a C++ pointer-to-const (const T*).
For example, something conceptually similar to:
POINTER TO CONSTANT ST_MyData
or
REFERENCE TO CONSTANT ST_MyData
The important distinction is that the reference itself may still be passed around or reassigned, but the referenced object cannot be modified through that reference.
CODESYS currently supports VAR_IN_OUT CONSTANT, which provides read-only reference semantics for parameters, but there does not appear to be an equivalent type that can be stored in a variable or returned from a method.
A practical use case is a registry that owns long-lived definitions.
The registry needs to retain ownership of the data while downstream systems maintain persistent references to it.
Currently, the main options are:
Return the struct by value, which loses reference semantics and requires a copy.
Return POINTER TO ST_Definition, which allows consumers to modify registry-owned data.
Convert the structure into a Function Block and expose a getter-only interface, which provides the desired encapsulation but introduces more complexity and memory overhead.
A read-only reference/pointer type would allow libraries to expose persistent, zero-copy access to internally owned data while preserving encapsulation.
This would be particularly useful for library APIs and object-oriented CODESYS architectures where ownership of data should remain with one component while other components are allowed to observe it.
Similar requests for constant references/pointers have been discussed on CODESYS Forge previously, so this appears to be a recurring use case rather than something specific to one application.