[fpc-devel] .NET Reflection vs RTTI

Daniël Mantione daniel.mantione at freepascal.org
Tue Nov 20 11:44:50 CET 2007



Op Tue, 20 Nov 2007, schreef Marco van de Voort:

> > Op Tue, 20 Nov 2007, schreef Marco van de Voort:
> > > > calling convention)
> > > > No usable type information for published fields (only a name to offset
> > > > mapping is given, no type information at all)
> > > > And so on...
> > > 
> > > Note that all this can be remedied (and better) by a simple code generator
> > > (e.g. codesmith), as long as you alone control the project.
> > 
> > Ok, but the question to be asked is why would you want to write a code 
> > generator if the compiler can generate usable RTTI for you?
> 
> RTTI is only half (you need to write the RTTI inspecting code anyway), the generated code is
> well integrated, and because there is an higher abstraction level you can
> perform checks.

I don't understand what you mean here. If you want to inspect types at 
runtime, of course you need to write inspection codes. Seems a tautology 
to me :)

> > If you want you can say the whole concept of RTTI is nonsense, since you 
> > can make those data structures yourself. However, the fact that the 
> > compiler generates them, saves you the work of writing and maintaining 
> > those RTTI data structures.
> 
> I'm not against RTTI, but it simply must not be overrated.

It should also not be underrated. It can save people mountains of work.

Pascal is a statically typed language. Therefore things like 
record1.array1[0].field3 only work for data structures known at compile 
time.

Often, the data expected is known at compile time, but it is exposed by
dynamic datastructures. I.e. retrieve a record from a database, it is known at 
compile time which fields you will get and what type they are. Yet you 
retrieve them using a dynamic interface.

RTTI allows one to convert dynamic data structures into static ones, 
without writing code for each individual stituation, here you have the 
mountains of work saved. I.e. you could write a procedure that converts 
any SQL record to any Pascal record, then use such a record with all 
comforts static data structures give.

Requests for RTTI on arrays and records are IMO completely logical.

> > > The .NET framework however also needs these attributes to interact with
> > > Windows server components as IIS, and I can also imagine this, when used
> > > sparingly applies to other server component vendors (hi Thorsten) too.
> > 
> > Attributes are IMO syntactic sugar for:
> > 
> > const datatype1_attributes:Tdatatype1_attributes=(
> >         rtti:typinfo(datatype1),
> >         attribute1:value1,
> >         attribute2:value2,
> >         attribute3:value3
> >       );
 
> In a statically compiled language, yes. .NET probably pulls them out of the
> JIT context for the relevant assembly when needed. Can do an extra JIT pass
> even if it needs them.

Yes.
 
> I don't know what you are getting to with the rest of your msgs.

If people are continuously writing such attribute data structures like 
above, it could make sense to give them some syntactic sugar. However, as 
I never encountered attribute data structures like above, it makes no 
sense to enhance the language with a feature no one need.

Daniël


More information about the fpc-devel mailing list