Strange Allowed Apex Syntax

Found some crazy apex syntax and I have no clue what it does.

public Account<List<String<Contact>>> whatIsWrongWithApex;

Is this a defect? Should this do something? Compiler not strict enough?

For the sake of the question, here is a simpler example:

public String<String> iThoughtThisShouldOnlyWorkForCollections;

Edit: Here is a full example where I instantiate an Account using this method and successfully insert it into the database:

public class ABug {
  public Account<List<String<Contact>>> whatIsWrongWithApex;

  public ABug() {
    whatIsWrongWithApex = new Account<List<String<Contact>>>();
    whatIsWrongWithApex.Name = 'Account Name';
    insert whatIsWrongWithApex;
  }
}

Anonymous Apex:

new ABug();

Answer

This ‘strange’ syntax is a result of native Java support for Generics.

We cannot leverage the power of generics in Apex just yet, but the platform itself most certainly does. This is likely being exposed here through the definition & inheritance of the sObject class.

Java Generic Types

Attribution
Source : Link , Question Author : Phil Rymek , Answer Author : Greg Grinberg

Leave a Comment