3

Is there a way to use nested attributes in Delphi? At the moment I'm using Delphi XE.

For example:

TCompoundAttribute = class (TCustomAttribute)
public
  constructor Create (A1, A2 : TCustomAttribute)
end;

And the usage would be

[ TCompoundAttribute (TSomeAttribute ('foo'), TOtherAttribute ('bar')) ]

At the moment this leads to an internal error. This would be a nice feature to create some boolean expressions on attributes.

menjaraz
  • 7,551
  • 4
  • 41
  • 81
Christian Metzler
  • 2,971
  • 5
  • 24
  • 30
  • 1
    If it leads to an internal error, apparently there is no way. – Rudy Velthuis Aug 24 '11 at 10:32
  • 2
    At the moment this seems to be true. But the question is, whether it is by design or not. And if not, is it a bug? Will it be supported in future versions? – Christian Metzler Aug 25 '11 at 10:15
  • I don't know if it is by design. An internal error is obviously not by design, but I don't know if they ever want to be able to nest attributes. Only Embarcadero's Delphi compiler team can answer that. – Rudy Velthuis Aug 25 '11 at 10:24
  • Delphi compiler team seems to be busy with getting XE2 ready to ship. No time for SO I guess :) – jpfollenius Aug 27 '11 at 22:24
  • 1
    Can you give some situation where nested attributes fit ? – menjaraz Dec 16 '11 at 06:25
  • See http://stackoverflow.com/questions/8637044/nested-attributes-in-delphi-continued/8646357#8646357 for an example in the Java programming language – mjn Dec 29 '11 at 10:52

1 Answers1

0

I think you mean default attributes of create method.

Something like this should be working:

unit Unit1;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFoo = class
  private
    FA1: string;
    FA2: string;
    { Private declarations }
  public
    procedure Show;
    constructor Create (a1: string = 'foo'; a2: string = 'bar');
  end;

var
  o : Tfoo;

implementation

{$R *.dfm}

procedure Tfoo.show;
begin
  ShowMessage(FA1 + ' ' + FA2);
end;

constructor Tfoo.create (a1: string = 'foo'; a2: string = 'bar');
begin
  FA1 := a1;
  FA2 := a2;
end;


begin
  o := Tfoo.create;
  o.show;   //will show 'foo bar'
  o.Free;

  o := Tfoo.create('123');
  o.show;   //will show '123 bar'
  o.Free;

  o := Tfoo.create('123', '456');
  o.show;   //will show '123 456'
  o.Free;

  //etc..
end.
biegleux
  • 13,179
  • 11
  • 45
  • 52
KaMM
  • 51
  • 1
  • 5
  • 1
    No. :-) The question is not about `default parameters`; it's about `attributes`. Read the sample usage again; it has nothing to do with your answer's topic. – Ken White Sep 11 '12 at 18:31