DelphiAST icon indicating copy to clipboard operation
DelphiAST copied to clipboard

Generic Methods

Open Wosi opened this issue 8 years ago • 3 comments

Generic type parameters are plain text part of method names. With this PR type parameters of methods are part of AST in the same way as they are in type declarations.

Code:

TGen<T> = class
  procedure Generate<T2>;
end;

AST:

<METHOD begin_line="15" begin_col="5" end_line="16" end_col="3" kind="procedure" name="Generate">
    <TYPEPARAMS line="15" col="23">
        <TYPEPARAM line="15" col="24">
            <TYPE line="15" col="24" name="T2"/>
        </TYPEPARAM>
    </TYPEPARAMS>
</METHOD>

We may need to discuss how the implementations of generic methods are shown in AST.

Code:

procedure TGen<T>.Generate<T2>;
begin
  
end;

Currently the method's name attribute contains the entire method name TGen<T>.Generate<T2>. This is not the most elegant solution as someone would need to parse the name in order to get the generic type parameters and the name of the current class. I didn't want to break existing projects using DelphiAST so I decided to remove only the method's generic parameters from the name and keep the rest of it untouched: The type name including its generic parameters remains still plain text.

AST:

    <METHOD begin_line="170" begin_col="1" end_line="175" end_col="1" name="TGen&lt;T&gt;.Generate" kind="procedure">
      <TYPEPARAMS line="170" col="27">
        <TYPEPARAM line="170" col="28">
          <TYPE line="170" col="28" name="T2"/>
        </TYPEPARAM>
      </TYPEPARAMS>
      <STATEMENTS begin_line="171" begin_col="1" end_line="173" end_col="4"/>
    </METHOD>

Wosi avatar Aug 31 '17 11:08 Wosi

Can we keep both? Full method name as a string and also as a subtree?

RomanYankovsky avatar Aug 31 '17 14:08 RomanYankovsky

Generally yes. I'd like to know what other people think.

Wosi avatar Aug 31 '17 15:08 Wosi

This is implemented in #231. It works pretty much as expected.

JBontes avatar Oct 03 '17 17:10 JBontes