Consider operator proposal (original) (raw)

Marek Kozieł develop4lasu at gmail.com
Sat Mar 14 13:24:27 PDT 2009


AUTHOR: Lasu aka Marek Kozieł

OVERVIEW

FEATURE SUMMARY: Consider operator extends language with full value support and assign value to name and return it.

MAJOR ADVANTAGE: It introduces natural division in variables and values. It makes language to be more natural for human and generics easier to use.

MAJOR BENEFIT(s): If we need to get a value, we can do that quite simple (without splitting expression or duplicating code). Decrease number of potential bugs (value cannot be assigned). It can help in splitting code to proper parts; nowdays, we need to declare variable regardless we need it or not (or make it final). Decrease size of unnecessary code. Method can be itself multi-thread with this, but it's far future.

MAJOR DISADVANTAGE: Possibility to be unclear at start.

ALTERNATIVES: Some time 'final' variables can be used, but it's more like for-each, life is better with it.

EXAMPLES

SIMPLE EXAMPLE: public static void standard(Resource<String,Container> resource) { Comparator<Resource<String,Container>> resourceComparator = (standard? Resource.baseComparator : Resource.extdComparator); if (resourceComparator.compare(resource,this.last)==0) return; // duplicate ... }

public static void consider(Resource<String,Container> resource) { (standard? Resource.baseComparator : Resource.extdComparator) :: resourceComparator; if (resourceComparator.compare(resource,this.last)==0) return; // duplicate ... }

public static void standard_2(ArrayList<Resource<String,Container>> resources){ for (Resource<String,Container> resource: resources){ System.out.println(resource); } }

public static void consider_2(ArrayList<Resource<String,Container>> resources){ for (::resource:resources){ System.out.println(resource); } }

ADVANCED EXAMPLE: public void validLastElementState(ArrayList list){ if (list.get(list.size()::size-1)::data.reportStare().getState()!=validState){ throw new IllegalStateException(Data.class.getSimpleName()+"("+data+") of "+size+" elements is in wrong "+State.class.getSimpleName()); } ... other operations ... }

Would be compiled to: public void validLastElementState(ArrayList list){ { list.size()::size; list.get(size-1)::data if (data.reportStare().getState()!=validState){ throw new IllegalStateException( Data.class.getSimpleName()+"("+data+") of "+size+" elements is in wrong "+State.class.getSimpleName()); } } ... other operations ... } DETAILS

SPECIFICATION: Value :: name Assign left side value to given name and return it. JLS 14.14.2: http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2 EnhancedForStatement: for ( VarDeclaration : Expression) Statement

VarDeclaration: al least one of VariableDeclaration ValueDeclaration

VariableDeclaration VariableModifiersopt Type Identifier

ValueDeclaration :: ValueName

COMPILATION: While value is same as final variable compiling is not big problem.

TESTING: Same as final variable.

LIBRARY SUPPORT: No.

REFLECTIVE APIS: No.

OTHER CHANGES: No.

MIGRATION: No need.

COMPATIBILITY Compiled code can be normally executed.

REFERENCES Consider operator proposal: http://lasu2string.blogspot.com/2009/03/consider-operator-proposal.html

-- Pozdrowionka. / Regards. Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the coin-dev mailing list