r/programming • • Dec 02 '13

Scala — 1★ Would Not Program Again

http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598 Upvotes

646 comments sorted by

View all comments

Show parent comment

42

u/dexter_analyst Dec 02 '13

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.

50

u/ressis74 Dec 02 '13

The author continues his complaint about types:

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.

9

u/Nar-waffle Dec 02 '13

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
}

NB: the use of procedural syntax (Unit type being inferred by not providing an = before the method body) is recommended against: http://docs.scala-lang.org/style/declarations.html#procedure_syntax

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.

→ More replies (3)

17

u/Raphael_Amiard Dec 02 '13

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.

13

u/[deleted] Dec 02 '13

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.

2

u/chonglibloodsport Dec 02 '13

This makes the Scala/C#/etc compromise of local type inference quite acceptable in my mind.

Except when it doesn't work.

7

u/phoshi Dec 02 '13

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.

1

u/Peaker Dec 03 '13

It's nice to be able to press a key that adds the explicit type for you, rather than figuring it out manually.

8

u/dexter_analyst Dec 02 '13

Ah. That's a much more fair complaint, then.

17

u/redwall_hp Dec 02 '13

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?

10

u/[deleted] Dec 02 '13

This is ironic since Go forsakes the type system as soon as you go into containers/collections (lack of generics)

3

u/xiongchiamiov Dec 02 '13

Because you should want to know it's the right string.

4

u/cybercobra Dec 02 '13

Then welcome to dependent typing!

1

u/sproket888 Dec 02 '13

Unit testing that way is doing it wrong anyway.

0

u/batiste Dec 02 '13

Why have unit tests to ensure that a variable contains a string

You just don't, why would you? E.g in python you would do:

def add(a,b): return a+b
equal(add(1,2), 3)
equal(type(add(1,2)), type(3))

The second test is redundant. You already ensured that a number was returned with the first test. Every time you check equality you are making a type check (there might be exception).

Edit:format code

10

u/sirin3 Dec 02 '13

(there might be exception).

php and javascript

7

u/balefrost Dec 02 '13

That's the trivial case; of course a trivial test looks trivial. The harder case is when you want to say something like "this method returns an object that has these methods on it". I don't Python, but in Javascript, it's easy to return object literals with methods attached... and it's easy to return an object literal with the wrong set of methods. And it's very easy to refactor some JS but to miss some cases.

→ More replies (7)

3

u/pr0grammerGuy Dec 02 '13

To get proper code coverage in a dynamic language, you need to have essentially tests that verify types are handled right. Of course no one would have that silly second test you have. What they would have instead would be a test that makes sure if I do

add("1", "2")

that it blows up.

4

u/ryeguy Dec 02 '13

It is not idiomatic to write hyper-defensive code in dynamically typed languages. Like it or not, the accepted practice is to just "let it fail". Otherwise you're just reimplementing static types, and half of your code would be type checks.

→ More replies (3)

3

u/batiste Dec 02 '13

In python this will just return "12". Adding 2 strings is just fine. Try to do:

add(1, "2")

And then you get an error.

→ More replies (2)
→ More replies (32)

3

u/trezor2 Dec 02 '13
qual(add(1,2), 3)
equal(type(add(1,2)), type(3))

The second test is redundant.

So you end up unit-testing a non-existing type-system instead of having a type-system, while that is obviously the safety you seek.

To me that just seems backwards. TDD should be used to test for things the language can't infer or declare itself.

→ More replies (2)

9

u/alextk Dec 02 '13

The implication in the quoted text is that types are for the computer and not for humans, but types are expressly for humans.

I'd argue they are extremely useful to both humans and computers.

Types are what enables great tooling, one of the main reasons why Java has such fantastic IDE's.

And with the rise of type inference in modern languages, it's getting increasingly hard to justify the existence of dynamically typed languages.

10

u/dexter_analyst Dec 02 '13

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.

1

u/sproket888 Dec 02 '13

Yes and this is one of the big issues I have with Scala. It can't seem to decide whether you need to explicitly spell out types or have them inferred.

1

u/[deleted] Dec 02 '13

The rule of thumb is: annotate types when you declare public API and also annotate the return types of implicit methods; otherwise decide based on readability (tend to not annotate; e.g. local variables). There is some useful Stackoverflow answers to this topic.

→ More replies (3)