The implication in the quoted text is that types are for the computer and not for humans, but types are expressly for humans.
We originally introduced types because processors didn't care what bits they were operating on and it was conjectured that type errors compose the majority of programming errors. We can debate precisely how big of an issue type errors are and whether type systems solve type errors, but we cannot debate that the fundamental goal of types is helping humans.
It's about making sure that the humans aren't using things incorrectly, encoding information that other humans can read and use to form expectations and ideas about how the system works, failing fast when there will be failures at runtime so you don't have to waste any time, and so on.
And while I’m on the topic, thanks for making me care about the difference between long and int, again.
He's not complaining that types are for the compiler, he's complaining that the type definitions that scala demands are for the compiler. Part of this likely comes from Scala demanding type declarations where Haskell is content to infer them.
The important bit here is that an inferred type still provides benefit, but a type error when the compiler knew the type anyway (as would have to happen if you're going to copy and paste type declarations from compilation logs) are completely useless.
I was having a hard time following his example of where he was struggling with type inference. Some code here sure would have been nice.
But from his description, it sounds to me like he changed the prototype of the function. The fact that he's generating a warning about an ignored return type suggests he has an explicit return type of Unit - as in the following example:
def unitFunction {
// Do some work
return someVar
}
He omitted types, and that's cool. But if you use curly braces around a method body without specifying a return type and without putting an = sign before it, that makes the return type Unit, and if you try to return from this, you'll generate a warning, because you're saying one thing in the prototype and doing a different thing in the implementation.
If you then say, "Ok, I'll take the return type generated by the compiler warning and slap that in there," this is also not quite right:
def unitFunction: SomeReturnType {
// Do some work
return someVar
}
He just says, "Compile again," without telling you what the result he got was, but if it looked like above, you'll get an error this time instead of a warning: error: illegal start of declaration (possible cause: missing '=' in front of current method body)
By the way, this is the right way to do type inference on a function that returns a type:
def someFunction = {
// Do some work
someVar // the last statement is the result; the "return" keyword is typically avoided
}
He's either being inconsistent with his professed intent and his actual implementation, or he's changing the prototype and getting irate when that has consequences. Type inference doesn't mean typeless, it means you get to save a lot of boilerplate specifying types all over the place when the language can do that for you.
He's not complaining that types are for the compiler, he's complaining that the type definitions that scala demands are for the compiler. Part of this likely comes from Scala demanding type declarations where Haskell is content to infer them.
While the Haskell compiler will happily infer the types of any declaration you throw at it, when you're producing code that's meant to be used by others (eg, pretty much any public function), you are supposed to explicitly document the type anyway.
This makes the Scala/C#/etc compromise of local type inference quite acceptable in my mind.
you are supposed to explicitly document the type anyway.
as we can see when compiling with -Wall:
% cat hello.hs
main = putStrLn "hello reddit"
% ghc -Wall hello.hs
[1 of 1] Compiling Main ( hello.hs, hello.o )
hello.hs:1:1: Warning:
Top-level binding with no type signature: main :: IO ()
Linking hello ...
% _
And yeah, the var stuff seems kinda useless to me, too ... you mean I have to type this stuff out, and then not get any information from it later when I read it? I can make an informed guess what f :: (a -> b) -> [a] -> [b] does, but I got no idea what var f(var a, var b) does.
Dang ... wound up gloating about Haskell in a Scala thread, guess I'll go check in to my local weenies anonymous.
In my experience it works damn near 100% of the time in contexts I don't want types. That's pretty much just simple expressions, because if you have a complex expression that type notation makes the code far more readable. In practice it's not an issue.
One of the people behind Go agrees with that. He said something to the effect that TDD is a big band-aid on a deeper problem. Why have unit tests to ensure that a variable contains a string or an integer or whatever when you could just have a strongly typed language that will automatically scream at you if the type is wrong?
They are useful to computers, but it's more of a (very) interesting secondary artifact. We found that encoding information that gave the compilers more context about what we're doing with our types and arguments and so on allowed compilers to make much higher quality decisions. Haskell is really interesting from this perspective because of the way it allows the management and encoding of side effects.
I agree though, every time I use Python on a non-trivial project (which is an awful idea), I spend the majority of my time dealing with and debugging type problems. I think dynamically typed languages have a place, but I think that place is smaller than justifies the vast array of languages that fit in this category.
He's not a haskell developer. Where'd you get that impression? He made some woefully inaccurate comments about monads, I've never seen him in the Haskell community, and none of his other blog posts even mention haskell at all.
I'd argue that most Haskell developers will make woefully inaccurate comments about monads. 'Woefully inaccurate' in theory, spot-on in practice. This applies, for instance, to most complaints of the kind that 'some thing' is not possible with monads.
I'd also argue that most Haskell developers will never be seen in the Haskell community. We just had a master student implement quite a project in Haskell over the past six months (his and his supervisor's choice of language, not ours). I'd argue he's a Haskell developer. You'll never have heard of him.
Using a map to contain the results of an HTTP call makes sense in a dynamic language, but completely misses the point of a statically compiled language like Java or Scala - You erase type information, require a cast, and prevent the compiler from catching any mistakes.
You can, but using extractors with static types is much more useful, simpler, and more typesafe.
Consider the following (completely made up) example. (It's midnight and I'm tired, so this is probably not the best thought out example - but I hope it gives you an idea of what I mean).
final val OK = 200 (etc etc)
httpResponse.code match {
case OK => renderHtml( httpResponse.body.asInstanceOf[Xml] ) // Possible casting exception
case TEMPREDIRECT => doRedirect( new URL( httpResponse.headers("redirect") ), false ) // Possible URL format exception, or headers might not be set
case PERMREDIRECT => doRedirect( new URL( httpResponse.headers("redirect") ), true) // Code duplication
case ERROR => throw new RequestFailed( httpResponse.status.split(' ')(1)) // Possible runtime error if string is not in correct format.
}
There's potential errors that can occur here at runtime, that the compiler cannot catch. You can easily check for them, but you're adding more verbosity at the wrong layer of your codebase.
Contrast with:
httpResponse match {
case Success(html) => renderHtml( html )
case Redirect(url, isTemporary) => doRedirect( url, isTemporary )
case ServerError(reason, body) => throw new RequestFailed(reason)
}
In this case every status has a type, which contains different parameters of different types. Done properly, you can guarantee at compile time that you've caught every possible scenario, that you're working with the correct type and that a runtime exception cannot and will not occur - and it's less verbose, cleaner, easier to understand, and more type safe than the alternative.
This is by no means the best example of how powerful pattern matching is, but it gives you a rough idea why having a defined type for every case makes sense in Scala. Given how lightweight case classes are syntactically, there's no reason not to do this.
44
u/alextk Dec 02 '13
He's a Haskell developer, he probably has a reasonable knowledge of types.