> > Cedric Beust wrote > > In a dynamically typed language, you have to write tests > to catch type mistakes (such as the one described by James > earlier, where he forgot a * in front of a variable, > thereby declaring it as a literal instead of an array). > These tests are unnecessary in statically typed languages > s since they are caught by the compiler. > > On the other hand, I can't think of a test written in a > statically typed language that would be made unnecessary > in a dynamically typed language because of the fact that > types are optional in that language.
I can.
Try the following in Java:
assertEquals("10000000000", (new Double(Math.pow(10, 10)).intValue()));
Ruby gives the expected result even for very large numbers:
10.power!100000
There are other problems. If Java were truly static you wouldn't need to cast. The following compiles in Java but throws a RuntimeException:
List a = new ArrayList(); a.add(new SomeObject()); for ( Iterator iter = a.iterator(); iter.hasNext(); ){ AnotherObject value = (AnotherObject )iter.next(); }
And generics won't help all time. The following compiles but generates a ClassCastException:
List artists = new ArrayList(); artists.add(new Artist()); List<Album> albums = new ArrayList(artists); for (Album album : albums) { album.getGenre(); }
In Java you can do:
"" + new Object()
Which might not give the expected result. In Ruby this creates an error.
p "Object " + Object.new => TypeError: can't convert Object into String