Forum > General
Some observations on the use of RTTI
vfclists:
--- Quote from: Warfley on October 07, 2024, 09:36:33 pm ---
--- Quote from: vfclists on October 07, 2024, 06:28:18 pm ---Do you know of any examples which are less UI oriented?
--- End quote ---
There are two man use cases for RTTI. First the most obvious one is data serialization and deserialization. For example, if you want to take a record and serialize it into json, right now you have to do it like this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---jsonObject.Strings['MyField1'] := MyRec.MyField1;jsonObject.Integers['MyField2'] := MyRec.MyField2;jsonObject.Arrays['MyArrayField'] := SerializeArray(MyRec.MyArrayField);Extremely annoying and error prone. With Extended RTTI, where field names are written into the RTTI, you could write a serializer that will just take all public fields and writes them down, recursively descending into other records or arrays. Then it would just be:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---SerializeRecord(@MyRec, TypeInfo(TMyRec));And when you update your record, rename fields, etc. the serializer will still work because of RTTI.
This is especially useful combined with the custom attributes, where you could do things like that:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type TMyRecord = record [SerializeName('Field1'), SerializeOptional('Default')] FField1: String; [SkipSerialize] FHiddenField: Integer; //...so SerializeName would be an attribute to have a custom name, e.g. for backwards compatibility, or to not have the leading F as in this case. SerializeOptional could be an attribute to say when deserializing and this object is not there, fill it with 'Default'. SkipSerialize would tell the serializer that FHiddenField is an internal field not to be serialized into json.
This way you do not need to write a single line of (error prone) code to convert your data into other formats, as all the required information can be encoded into the definition of the type.
The second main use case is reflection. Basically altering the behavior of your code at runtime. For example you could have a module system where you scan your filesystem for a module (e.g. in form of a dynamic library) and if a new module is found the module is loaded and RTTI is used to e.g. load these function pointers into existing objects/classes at runtime without having to restart the application.
For example you could create a software where classes objects have logger fields, and on startup, the module loader would look if there is a logger module available, and if so, scan the classes, if any has a logger field, and dynamically load the logger into that field.
--- End quote ---
Is the kind of ExtendedRTTI you speak of available in current FreePascal?
Warfley:
In trunk yeah, but not yet stable
Fibonacci:
Another issue with RTTI is that it facilitates reverse engineering of the program. It's extremely frustrating to see all type names (classes, records), unit names, and even the program name (from the first line of code). There should be an option to limit RTTI to the absolute minimum when it's not used.
Joanna from IRC:
--- Quote from: Fibonacci on October 20, 2024, 03:50:56 pm ---Another issue with RTTI is that it facilitates reverse engineering of the program. It's extremely frustrating to see all type names (classes, records), unit names, and even the program name (from the first line of code). There should be an option to limit RTTI to the absolute minimum when it's not used.
--- End quote ---
That concerns me too. Does anyone know what was the last version of fpc before rtti was introduced?
vfclists:
--- Quote from: Fibonacci on October 20, 2024, 03:50:56 pm ---Another issue with RTTI is that it facilitates reverse engineering of the program. It's extremely frustrating to see all type names (classes, records), unit names, and even the program name (from the first line of code). There should be an option to limit RTTI to the absolute minimum when it's not used.
--- End quote ---
I believe this depends on some {$M+} compiler directive which is set by default.
Others may know more.
Navigation
[0] Message Index
[#] Next page
[*] Previous page