In the department of wishful thinking:

In the department of wishful thinking:

type
TElement = class(TSomeClass)
Sibling: TElement;
end;

type
LinkAttribute = class(TCustomAttribute)
FLink: TElement;
constructor Create(aLink:TElement);
end;

TContainer = class

X1: TElement;

[Link(X1)] //<-- wish I could use an attribute to link X1 with Y1
Y1: TElement;

X2: TElement;

[Link(X2)] //<-- wish I could use an attribute to link X2 with Y2
Y2: TElement;

end;

It would be nice if attributes could reference class/record local fields or properties.
I know - it would put a lot of load on the compiler / linker.

If the compiler was two pass, order would not be important.
Since the compiler is single pass - perhaps something like

TContainer = class

[Link(forward Y1)]
X1: TElement;
Y1: TElement;

[Link(forward Y2)]
X2: TElement;
Y2: TElement;
end;

could be an option as well.

#MoreDeclarationLessCode

Comments

  1. Isn't this something that would be done in C++ through inline constructor initializers? Perhaps something similar would be a better option to add to Delphi?

    ReplyDelete
  2. What's the real world use-case for this?

    ReplyDelete
  3. Asbjørn Heid
    - Binding related time/value series for SQL queries.
    - Linking controls with value fields
    - Associate a handler with an event

    Basically, it would be like visual live binding with compile time validation instead of runtime validation.

    ReplyDelete
  4. Lars Fosdal I don't see how your proposal would work. It seems what you really want to do is to have

    type
    PPElement = ^TElement;
    LinkAttribute = class(TCustomAttribute)
    FLink: PPElement;
    constructor Create(aLink:PPElement);
    end;

    ...

    X1: TElement;

    [Link(@X1)]
    Y1: TElement;

    otherwise how do you envision this working?

    ReplyDelete
  5. Asbjørn Heid - Yes, that is basically what I want. In an instance of a class - any reference is relative to the base instance. Hence the compiler magic needs to inject the correct reference wrapper code for access (self + offset / self VMT resolve). I am not saying it would be trivial, but it sure would be nice.

    ReplyDelete
  6. Lars Fosdal Sounds quite complicated to implement. Couldn't you settle for having NameOf implemented? Then you can access the member at runtime through RTTI?

    So it would be
    [Link(NameOf(X1))]
    Y1: TElement;

    Then you could write some helpers to extract the value of the link element attribute at runtime, given an object instance.

    ReplyDelete
  7. Asbjørn Heid That could work, even if it is a bit of a workaround. Perhaps a new argument type would be an idea?
    Procedure Link(named aRef:string) ;

    ReplyDelete

Post a Comment