Proposal: Type inference for variable definition/initialization using the 'auto' keyword.

Tim Lebedkov tim.lebedkov at googlemail.com
Fri May 15 11:27:38 PDT 2009


Hello Joe,

see my comments below

On Fri, May 15, 2009 at 7:40 AM, Joseph D. Darcy <Joe.Darcy at sun.com> wrote:
> Tim Lebedkov wrote:
>>
>> Type inference for variable definition/initialization using the 'auto'
>> keyword.
>>
>> AUTHOR(S): Tim Lebedkov
>>
>> OVERVIEW
>>
>> Provide a two sentence or shorter description of these five aspects of
>> the feature:
>>
>> FEATURE SUMMARY: Should be suitable as a summary in a language tutorial.
>>
>> This proposal addresses the addition of type inference for
>> variable definitions to the Java programming language using the 'auto'
>> keyword instead of specific type name.
>>
>> For example, consider the following assignment statement:
>>
>> Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
>>
>> This is rather lengthy, so it can be replaced with this:
>>
>> auto anagrams = new HashMap<String, List<String>>();
>>
>> and anagrams is automatically typed as HashMap<String, List<String>>
>>
>> MAJOR ADVANTAGE: What makes the proposal a favorable change?
>>
>> Generics have had a tremendously positive impact on type safety in the
>> Java programming language. They  have made it possible to provide
>> static guarantees about the type of instances used by other classes,
>> preventing entire classes of runtime errors from occurring. However,
>> generics have added complexity to Java, making the code far more
>> verbose and occasionally difficult to read. Although solving this
>> problem is well outside the scope of this proposal, a limited form of
>> type inference would remove unnecessary redundancy from the language.
>> Even without generics it seems unnecessary to duplicate type names for
>> simple variable definitions/assignments like:
>>
>> Integer a = new Integer(1023);
>>
>> MAJOR BENEFIT: Why is the platform better if the proposal is adopted?
>>
>> Less code and no duplication has a positive impact on many things:
>> - faster typing/faster code changes
>> - easier code changing without IDE assistance
>> - code versioning diffs are more clear
>>
>> MAJOR DISADVANTAGE: There is always a cost.
>>
>> - it could be harder to read the code.
>> - auto will be a keyword and may break some old code
>>
>> ALTERNATIVES: Can the benefits and advantages be had some way without
>> a language change?
>>
>> no
>>
>>
>
> Catching up on proposal comments, I think the core benefits of using "auto"
> or "final" on the left hand side for variables are achieved with the diamond
> operator that allows one to elide type parameters on the right hand side.
>  IMO, the diamond solution of specifying the variable type fully on the left
> and then specifying only the implementation type without redundant, repeated
> type parameters on the right is more in keeping with Java coding style.
>
> -Joe
>

your statement "the core benefits ... are achived with the diamond
operator" is just not true.
As an example I picked 2 classes from Java 6: java.util.ArrayList and
javax.management.ObjectName. "auto" can be useful in 3% or 4.5% of
code lines respectively (and this including comments and whitespace!).
Whereas "diamond" in 0% and 0.09%!

"diamond" just cannot help with something like: TableModel m = new
JTable().getModel();

Your second statement about "keeping Java coding style" is completely
unclear to me. Why is

List<String> a = new ArrayList<>();

more "Java" than this:

auto a = new ArrayList<String>();   ?




java.util.ArrayList statistics:
===============================

private static final long serialVersionUID = 8683452581122892189L;
int oldCapacity = elementData.length;
int oldCapacity = elementData.length;
int newCapacity = (oldCapacity * 3)/2 + 1;
ArrayList<E> v = (ArrayList<E>) super.clone();
E oldValue = (E) elementData[index];
E oldValue = (E) elementData[index];
int numMoved = size - index - 1;
Object[] a = c.toArray();
int numNew = a.length;
Object[] a = c.toArray();
int numNew = a.length;
int numMoved = size - index;
int numMoved = size - toIndex;
int newSize = size - (toIndex-fromIndex);
int expectedModCount = modCount;
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];

"auto": 18 out of 596 lines = 3% (including comments and whitespace)
"diamond": 0 out of 596 lines = 0% (including comments and whitespace)
		
javax.management.ObjectName statistics:
=======================================

int in_begin = _key_index + _key_length + 1;
int out_end = in_begin + _value_length;
private static final long oldSerialVersionUID = -5467795090068647408L;
private static final long newSerialVersionUID = 1081892073854801359L;
private static boolean compat = false;
GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
String form = AccessController.doPrivileged(act);
static final private Property[] _Empty_property_array = new Property[0];
private transient int _domain_length = 0;
private transient boolean _domain_pattern = false;
private transient boolean _property_list_pattern = false;
private transient boolean _property_value_pattern = false;
char[] name_chars = name.toCharArray();
int len = name_chars.length;
char[] canonical_chars = new char[len]; // canonical form will be same
int cname_index = 0;
int index = 0;
Map<String,Property> keys_map = new HashMap<String,Property>();
int property_index = 0;
final String ichar = ((c1=='\n')?"\\n":""+c1);
boolean value_pattern = false;
final String ichar = ((c1=='\n')?"\\n":""+c1);
final StringBuilder sb = new StringBuilder();
String[] keys = new String[nb_props];
final Map<String,Property> keys_map = new HashMap<String,Property>();
int len = sb.length();
char[] initial_chars = new char[len];
char[] canonical_chars = new char[len];
String[] tmp_keys = new String[nb_props];
Property[] tmp_props = new Property[nb_props];
int last_index = nb_props - 1;
int next   = startKey;
int endKey = startKey;
final int len = s.length;
final String ichar = ((k=='\n')?"\\n":""+k);
boolean value_pattern = false;
int next   = startValue;
int endValue = startValue;
final int len = s.length;
final char q=s[startValue];
final int len = val.length();
final char[] s = val.toCharArray();
final int[] result = parseValue(s,0);
final int endValue = result[0];
final boolean value_pattern = result[1] == 1;
final int len = key.length();
final char[] k=key.toCharArray();
final int endKey = parseKey(k,0);
final int slen = s.length;
final int plen = p.length;
final char[] d=domain.toCharArray();
final int len = d.length;
int next = 0;
final char c = d[next++];
final ObjectInputStream.GetField fields = in.readFields();
ObjectOutputStream.PutField fields = out.putFields();
Map<String,String> table = Collections.singletonMap(key, value);
Property prop = _ca_array[i];
String key = prop.getKeyString(_canonicalName);
int len = _ca_array.length;
final int total_size = _canonicalName.length() - _domain_length - 1-
(_property_list_pattern?2:0);
final char[] dest_chars = new char[total_size];
final char[] value = _canonicalName.toCharArray();
final int total_size = _canonicalName.length();
final char[] dest_chars = new char[total_size];
final char[] value = _canonicalName.toCharArray();
final int offset = _domain_length+1;
final int end = writeKeyPropertyListString(value,dest_chars,offset);
final char[] dest_chars = data;
final char[] value = _canonicalName.toCharArray();
int index = offset;
final int len = _kp_array.length;
final int last = len - 1;
final Property prop = _kp_array[i];
final int prop_len = prop._key_length + prop._value_length + 1;
int len = _canonicalName.length();
ObjectName on = (ObjectName) object;
final StringBuilder buf = new StringBuilder("\"");
final int len = s.length();
final StringBuilder buf = new StringBuilder();
final int len = q.length();
char c = q.charAt(i);
final char[] dom_pattern = getDomain().toCharArray();
final char[] dom_string  = name.getDomain().toCharArray();
final Map<String,String> nameProps = name._getKeyPropertyList();
final Property[] props = _ca_array;
final String cn = _canonicalName;
final Property p = props[i];
final String   k = p.getKeyString(cn);
final String   v = (String)nameProps.get(k);
final char[] val_pattern =p.getValueString(cn).toCharArray();
final char[] val_string  = v.toCharArray();
final String p1 = name.getCanonicalKeyPropertyListString();
final String p2 = getCanonicalKeyPropertyListString();
int domainValue = this.getDomain().compareTo(name.getDomain());
String thisTypeKey = this.getKeyProperty("type");
String anotherTypeKey = name.getKeyProperty("type");

"auto": 97 out of 2132 lines = 4.5% (including comments and whitespace)
"diamond": 2 out of 2132 lines = 0,09% (including comments and whitespace)


Regards
--Tim



More information about the coin-dev mailing list