One thing I noticed right off the bat, after dinking around for no more than 10 minutes, is that Sun got usage of the "+" operator correct, in my opinion, unlike it did with java. In other words, I hate the fact that in java the "+" operator gets used for mathematical addition AND string concatenation. I dislike this for two (well documented) reasons:
- String concatenation when performed in a loop, results in allocation of lots of unnecessary objects
- It leads to unexpected results when concatenating numbers and Strings and the programmer did not think to use String.valueOf() or the toString() method of the wrapper class of a primitive.
public class Main {
public static void main(String[] args) {
String ten = "10";
Integer five = 5;
Integer two = 2;
System.out.println(ten + five + two);
System.out.println(two + five + ten);
}
}
With the resultant output of:
1052Because of the order of operations, both of those lines with println produce "correct" results, but probably not the expected results of a developer who would write such a line. Any java developer worth their salt will know not to do anything of the like... but that did not stop a senior developer at my employer, who has very strong kung-foo, from making that very mistake when he was tired and hurried.
710
The language spec writers for JavaFX script apparently got wise and corrected this. For instance, the following in JavaFX Script
function add(foo: String, bar: Integer) {
var result = foo + bar;
println("{foo} + {bar} = {result}");
}results in the following compile time error+ cannot be applied to strings. Use a string expression "{foo}{bar}"The fact that JavaFX Script doesn't allow concatenation between a String and a non-String type unless you use an actual String expression such "{foo}{bar}", thus making the type conversion to a String explicit and not implicit, is a win, in my opinion.
No comments:
Post a Comment