by dkarl · 13y ago · 0 comments
I'd say the biggest challenge to Python in that niche is the emergence of type inference for statically typed languages. Not too long ago, the difference between a statically typed language and Python was
List<Foo> foos = new List<Foo>();
foos.add(new Foo(2));
foos.add(new Foo(4));
versus foos = [ Foo(2), Foo(4) ]
Now, a static language might look more like this: val foos = List(Foo(2), Foo(4))
As far as I know, there still isn't a statically typed language that matches Python's simplicity and low barrier to entry, but I say that as someone who knows little about Go. foos = [2, 4]
even the fact that the 2 and 4 refer to foos can be inferred, as long as Foo behaves like a number and you make it part of the Num typeclass. It's pretty cool!So, oddly enough, static typing can actually make your code less noisy rather than more.
> let maybe_use_haskell = [ Just 2, Just 4 ]
also, in you could do your original example in a single line,
List<T> ts = new List<T>(List.fromArray({T(2),T(4)}));
even though it's still messier than something with type inference.
def foos = [ new Foo(2), new Foo(4) ]There's also this classic bug:
def printOne(Collection c) {
if (c.empty) {
print("Collection is empty")
} else {
print(c.iterator().next())
}
}
Can you spot the bug? This code works for all Collections... except Maps. If c is a Map, Groovy translates c.empty to c.get("empty"). Constantly having to be on my toes to avoid stuff like that is a pain.