Or, in Haskell, it could even just be
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.
> import Data.Maybe
> 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.
Try Groovy!
def foos = [ new Foo(2), new Foo(4) ]I've been writing a lot of Groovy recently, and it feels like Scala without type checking. That's not the worst thing in the world, I'd rather just use Scala. With Groovy, I get a lot of the expressiveness of Scala, but the lack of static type checking combined with Groovy doing its best to be magical is a recipe for bugs. For example, in Groovy, you can overload methods accidentally without a warning, which can create a hell of a surprise when somebody passes a null value. Rather than throw an error, Groovy dispatches the call to one of the methods even if neither one is more specific than the other.
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.
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
versus Now, a static language might look more like this: 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.