[fpc-devel] RTTI generating

silvioprog silvioprog at gmail.com
Sat Sep 20 05:41:56 CEST 2014


On Fri, Sep 19, 2014 at 9:08 PM, Boian Mitov <mitov at mitov.com> wrote:

>  Hi Hans,
>
> I am sorry to disagree with you.
>

+1.


> The attributes in particular allow you to do real magic, and reduce your
> code by at least 4 times, and speed up your development up to 10 times.
>

I tested the new RTTI in Delphi XE and it reduced my typing alot. See:

(* credits: https://code.google.com/p/delphi-orm/ *)
var
  person: TPerson; oid: Integer;
begin
  person := TPerson.Create('Daniele', 'Teti',32);
  Session.Save(person);
  oid := person.id;
  person.Free;
...
  person := Session.Load<TPerson>(oid);
  WriteLn(‘Person name is: ‘, person.FirstName);
  person.Free;
end;

I'm using an approach like this in Android development (with Java:
http://ormlite.com/sqlite_java_android_orm.shtml).

We have done that in Delphi.
> I actually did a session in the PasCon few days on that.
> Here is example:
>
> TMyInterfacedClass1 = class;
>
> [CreateType( typeinfo( TMyInterfacedClass1 ))] // Specifies the default
> creation type as TMyInterfacedClass1
> IMyInterface = interface
>  ['{EB12CEBD-42C3-4259-AE2C-70267B67F454}']
>
>  function GetValue() : String;
>
> end;
>
> TMyInterfacedClass1 = class( TBasicInterfacedObject, IMyInterface )
> public
>  function GetValue() : String;
>
> end;
>
> TMyInterfacedClass2 = class( TBasicInterfacedObject, IMyInterface )
> public
>  function GetValue() : String;
>
> end;
>
> TMyNestedClass = class( TBasicObject )
> protected
>  [Default( True )] // FValue1 will be initialized with True
>  FValue1 : Boolean;
>
>  [Default( 11.5 )] // FValue2 will be initialized with 11.5
>  FValue2 : Real;
>
>  [AutoManage( TMyInterfacedClass2 )] // FInterfaceValue will be created as
> TMyInterfacedClass2
>  FInterfaceValue  : IMyInterface;
>
> public
>  property Value1 : Boolean               read FValue1;
>  property Value2 : Real                  read FValue2;
>  property InterfaceValue : IMyInterface  read FInterfaceValue;
>
> end;
>
> TMyClass = class( TBasicObject )
> protected
>  [Default( 100 )] // FValue1 will be initialized with 100
>  FValue1   : Integer;
>
>  [Default( 'Helo World!' )]
>  FValue2   : String; // FValue1 will be initialized with 'Helo World!'
>
>  [AutoManage] // FNested will be automatically created and destroyed
>  FNested   : TMyNestedClass;
>
>  [AutoManage( TStringList )] // FStrings will be automatically created as
> TStringList and destroyed
>  // [AutoDestroy] // - If AutoDestroy is used instead of AutoManage, the
> object will not be created but will be automatically destroyed!
>  FStrings  : TStrings;
>
>  [AutoManage] // FNested will be automatically created as TStringArrayList
>  FStringArrayList  : IStringArrayList;
>
>  [AutoManage]  // FInterfaceValue will be created as TMyInterfacedClass1
>  FInterfaceValue  : IMyInterface;
>
> public
>  property Value1 : Integer                   read FValue1;
>  property Value2 : String                    read FValue2;
>  property Nested : TMyNestedClass            read FNested;
>  property Strings : TStrings                 read FStrings;
>  property StringArrayList : IStringArrayList read FStringArrayList;
>  property InterfaceValue : IMyInterface      read FInterfaceValue;
>
> end;
>

I intend to do the following if the new RTTI  (of Delphi XE) come in FPC
2.8.0 (like Java annotations):

TPerson = class(TObject) // or TSerializableObject
published
  { @code(Id): Field used to store the ID of each *Person*. }
  ['integer', 'notnull', 'autoinc', 'primarykey']
  property Id: Int64;
  { @code(Name): Field used to store the name of each *Person*. }
  ['string:50', 'nonull', 'unique']
  property Name: string;
end;

And from this object above, my framework will generate the metadata for the
table creation, generation of HTML/JS forms, generation of object mappers,
generation of CRUD web actions with their REST paths, generation of
documentation and UML, among others.


> As you can see all the management of all the classes and interfaces is
> done without the need of even one line of code.
> No constructors, no destructors, nothing.
> This alone cuts vast amounts of code.
> This however only scratches the surface of the RTTI power. You have no
> idea about some of the other possibilities ;-) .
> The RTTI does not only work with properties and fields, but with functions
> as well, allowing easy implementation of RPC and network distributed
> execution.
> It is a whole ball game.
> Frankly some of the arguments you provide ware the same I have heard 20
> years ago when I programmed in C++ and people ware telling me I should be
> doing it in Assembler.
> They simply had no idea what power OOP had as example, as you probably
> have no idea what is the real power of modern RTTI when properly used.
>
> I am at the same time huge fan of strong typed languages. The advanced
> RTTI does not really mean the code should and will not be strongly typed.
> Indeed I have done my own RTTI API which is stronger typed than the one
> Delphi provides.


I was very curious to see your RTTI API. :-)

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20140920/1b72c708/attachment.html>


More information about the fpc-devel mailing list