PROPOSAL: 'forget' keyword
Marek Kozieł
develop4lasu at gmail.com
Sun Mar 29 12:58:27 PDT 2009
AUTHOR: Lasu aka Marek Kozieł
OVERVIEW
FEATURE SUMMARY:
'forget' keyword allows to erase variable from current context, or it
means that field should not be used.
MAJOR ADVANTAGE:
This change makes language be more WYSIWYG.
MAJOR BENEFIT(s):
- It makes people be able to erase 'variable' from current context,
while now it's:
--> impossible for final variables (only by comments),
--> impossible for arguments (for not final Object we can assign null),
--> impossible for fields.
--> local fields need more or 'less' artificial blocks (two lines
wasted and one indent level).
- Declaring that variable should not be used, or does not contain
valid information for further use will be possible.
- Code quality does not fall so drastically after we leave it,
comparing to '=null', 'only comments' or 'weird blocks'.
MAJOR DISADVANTAGE:
New keyword == Someone can have declared method/field/variable named 'forget'.
ALTERNATIVES:
It's already listed.
EXAMPLES
SIMPLE / ADVANCED EXAMPLE(s):
public static void main(final String[] args) {
System.out.println(Arrays.toString(args));
forget args; // 'args' cannot be used anymore
String[] args; // Error: Duplicate local variable args
...
}
public class Forget {
private ArrayList<String> criticKeys;
public void addKey(String newKey){
forget this.criticKeys; // use synchronized method ONLY
... // validate
... // add
}
}
public static void main(final String[] arguments) {
int sum = 0;
for (String s : arguments) {
sum += Integer.parseInt(s);
System.out.println(sum);
forget s; // OK
forget args; // OK: arguments variable is final
forget sum; // Error: cannot erase variable it it will still can be used
...
}
System.out.println(arguments.length); // Error: erased context
}
DETAILS
SPECIFICATION:
forget VariableIdentifier;
or
forget this. FieldIdentifier;
Forbid using given identifier in all followed expressions, in all
blocks in which variable could be used in current method scope.
If variable is not final then forget expression cannot occurs in loop
block if variable was declared in wider block, it's because variable
should not be forgotten if it will be used again.
If field is not final than forget expression cannot occurs in any loop
block (can be reconsidered).
COMPILATION:
Variable is not erased (but it can be, if it's not field), it's just
protected from access that should not happen for any reason. What's
more, handling variable, as if it still exists, prevents a situation
when a programmer creates new variable with the same name and then
removes forget statement.
TESTING:
It's the same as testing effects of exiting from the block for
variables. For fields, it's just an access control.
LIBRARY SUPPORT:
None.
REFLECTIVE APIS:
None.
OTHER CHANGES:
None.
MIGRATION:
None.
COMPATIBILITY
New keyword can be problem. There is not other impact.
REFERENCES
http://lasu2string.blogspot.com/2009/03/forget-keyword-proposal_27.html
--
Pozdrowionka. / Regards.
Lasu aka Marek Kozieł
http://lasu2string.blogspot.com/
More information about the coin-dev
mailing list