3 ms·
A well-designed type-inferred statically-typed language either cannot support the following (which is valid Python), or can only do so with tradeoffs that eithe
by geofft 12y ago
A well-designed type-inferred statically-typed language either cannot support the following (which is valid Python), or can only do so with tradeoffs that either stretch the definition of "statically-typed", stretch the definition of "well-designed", or would make the language even more intimidating than monads make Haskell:
def fizzbuzz(i):
if i % 3 == 0:
return "fizz"
else if i % 5 == 0:
return "buzz"
else:
return i
for i in range(100):
print(fizzbuzz(i))
Now you might argue that it's a good thing for people to care about proper typing for large, maintainable codebases, and I would agree; in some of the better Python code I've seen, it's social convention to add type signatures in a comment (I once worked at a place where people added Haskell type signatures) and enforce proper typing. And yes, there are a few Python APIs that return either a single item or a list, instead of a one-item list, and they're annoying.
But it's a thing people do, it's especially a thing that's useful in a teaching language or in a scripting language that's trying to fill PHP's niche, and I don't think a language that refuses to type-check such a thing will fit every conceivable need. Sometimes the goal is just to get something done quickly, not be large or maintainable.
- coldtea 12y ago>A well-designed type-inferred statically-typed language either cannot support the following (which is valid Python), or can only do so with tradeoffs that either stretch the definition of "statically-typed", stretch the definition of "well-designed", or would make the language even more intimidating than monads make Haskell A statically typed language can still have a generic type. C# for example has dynamic: https://msdn.microsoft.com/en-us/library/dd264736.aspx https://msdn.microsoft.com/en-us/library/dd264736.aspx, and Objective-C has id. You get static types whenever you need them and dynamic flexibility whenever you need it. No need to invoke something Haskelly at all. For more type checking safety in this particular case, you could also say (or infer) that the return type is a union that's either "String | Integer". I was talking about a possible very flexible uber-language here. Why would it miss those two oldest tricks in the book, that are already present in tons of existing languages? That said, I didn't say it would have to express all idioms. Just that it would be applicable for as many domains as possible, from low (drivers, etc), to high (scripting, etc). This particular idiom, as you also note, I don't find particular useful anyway, but rather a code smell. But if it was needed the two ways described above could solve it without much issue. When you need to be quick and flexible use "dynamic" type, when you want to be fast and safe and take little memory switch it to a specific type. The language still covers both cases.
- tome 12y agoI'm not sure what your complaint is. import Control.Monad (forM_) fizzbuzz i | i `mod` 3 == 0 = Left "fizz" | i `mod` 5 == 0 = Left "buzz" | otherwise = Right i main = forM_ [1..100] (\i -> either putStrLn print (fizzbuzz i))
- geofft 12y agoYeah, that certainly works, but one of Python's particular strengths (and in particular what the original article was about) is a teaching language. I certainly understand what you're doing there, but I wouldn't want to explain it on day 3 of high school CS class. :) The slightly cleaner thing here would be to take advantage of both strings and integers implementing Show. I'm thinking a bit about whether that would actually work well enough in a teaching language; possibly. (Although if I'm remembering my Haskell well enough, you need to enable existentially quantified types to make this work, which, again not really day-3 material. Probably works fine in like Rust, though.)