对于python的list,相信用过的人都知道有好些很cool的功能,我希望能把这些功能加入到java里面去。
我希望java能够支持一下的Python特性:
public class Main{
//list define
List k=[];
List k1=['1',2,3];
List k2=[k1,k2];
List<Integer> kk=[1,2,3,new Integer(4)];
//list assignment
k=[1,'2',3,4,"a","b",6];
k1=k;
//list get element
int a=k[2];
a=k[-1];
k=k[0:-1];
k=k[2:];
k=k[:2];
k=k[::-1]; //reverse array
k=k[1:5:2];// from element 1 to element 5, increase step is 2
//advanced assignment
list k3=[i*i for i in k if i <3];
//add
k=k+k1;
//get length
k.length();
//convert
k.toString();
//toArrayList
k.toarraylist()
}
这样,需要修改Javac的Grammer。
Java SE7的语法:http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html
进行了一些修改:
下标:(增加ListSubScript) Selector:
. Identifier [Arguments]
. ExplicitGenericInvocation
. this
. super SuperSuffix
. new [NonWildcardTypeArguments] InnerCreator
[ ListSubScript ]
ListSubScript:
'.' '.' '.' | Expression | [Expression] ':' [Expression] [SliceOP]
SliceOP: ':' [Expression]
变量定义和初始化:(增加ListInitializer和ListForInitializer)
VariableInitializer:
ArrayInitializer
Expression
ListInitializer
ListInitializer:
[ [ VariableInitializer { , VariableInitializer } [,] ] ]
ListForInitializer
ListForInitializer:
[Expression for Identifier in Expression [if Expression]]
列表相加和赋值:(在Primary里面加入ListInitializer)
Primary:
Literal
ParExpression
this [Arguments]
super SuperSuffix
new Creator
NonWildcardTypeArguments ( ExplicitGenericInvocationSuffix | this Arguments )
Identifier { . Identifier } [IdentifierSuffix]
BasicType {[]} . class
void . class
ListInitializer
来源:oschina
链接:https://my.oschina.net/u/574785/blog/89595