Live data from Hacker News

The advantages of static typing, simply stated

pchiusano.github.io

51–60 of 127 posts

Re: The advantages of static typing, simply stated

#51

Interestingly, most of these advantages are not specific to static typing, but derive from having a language that talks about types – even a dynamic one. For example, most of these advantages apply to Julia as well, a dynamic language that has type declarations, which: - Lets you create typed collections so that if you insert the wrong kind of value, you get an error immediately, albeit only when code runs, not befor…

How can you define throwing an error at runtime instead of compile time an advantage?

I didn't claim it was. That's one of the tradeoffs you make in exchange for a simpler, more forgiving programming model (and the ability to do serious work in a REPL or notebook). However, given that Julia programs can be mostly type inferred before execution, it is possible to check for this kind of error before running a program. There have been some projects to do exactly that [1] [2], and I suspect that once Julia reaches 1.0, we'll focus more effort on static analysis tools.

The point is that you don't need a static language to get many of the benefits of types, you just need your language that allows you to express type-based properties. One of the way to do that is to have a set of formal rules for deriving the type of every expression in a program, but that's not actually necessary.

[1] https://github.com/astrieanna/TypeCheck.jl

[2] https://github.com/tonyhffong/Lint.jl

Re: The advantages of static typing, simply stated

#52
post #29

It's frustrating that this static vs dynamic 'battle' still goes on. The longer everyone thinks this is actually a problem, the longer we have to wait for innovations to happen. Look at the web in 2016, the technology is a complete disarray. Look at the game industry in 2016, where C++ is thrown around as the the cause and solution to all life's problems. A language created 33 years ago now with no intention of being…

With static typing the computer doesn't "understand" your code. It just automatically checks the correctness proof you've presented to it. And the correctness is limited only to certain properties of the program, that a given type system supports.

> And the correctness is limited only to certain properties of the program, that a given type system supports.

In case there are readers not familiar with the current state of things, you can pick solid languages today whose type systems support a LOT. It is completely fair (and good) to point out that this isn't a panacea, but it can significantly help.

Re: The advantages of static typing, simply stated

#53
> Some tasks, especially around generic programming, can be very easily expressed in a dynamic language, but require more machinery in a static language.

I think this is not generally true and not a point against static typing, but rather against statically typed languages with poor support for generics. Java stands out as the poorest implementation of generics I have ever seen.

> For instance, a generic serialization library can be written in a dynamic language, without anything fancy, but providing the same thing in a static language requires more machinery, and is sometimes more complicated to use.

As a counterexample, have a look at NimYAML (my work):

    http://flyx.github.io/NimYAML/
The examples there show how easy it is to provide the user with a generic interface for serialization in a statically typed language. The implementation does not differ much from what you'd do in a dynamic language: Provide a pair of serialization/deserialization handlers for each of [simple types (string, int, float, enums), array/sequence types, tuple/object/struct types, dict/map types, pointer/reference types].

Re: The advantages of static typing, simply stated

#54
post #45

> Is it safe to call f(x)? The author asserts that static typing allows the compiler to answer this question, but this only allows the compiler to spot type errors in advance. There are many other kinds of errors that are completely invisible to the compiler. In a dynamically typed language, if I don't spot the error from reading the code, I must wait until runtime/testing to discover the error. This is also true for…

You are right - in a tautalogical way - that type systems only catch type errors. However, in modern languages (including Haskell, Scala, as well as newer, more experimental languages like Idris), those type errors can be extremely powerful.

Many people assume that 'types' are simply primitives like Int and String, and that a type checker just makes sure you don't pass an Int to a function expecting String. However, it is possible to express far more powerful statements about your data using a good type system.

For example, you can express the idea of non-emptiness of a container, as mentioned in the article. Then you know that, say, taking the max element of a non-empty container is guaranteed to give you an element, whereas with a possibly-empty container you might not have any element at all, causing a null, or exception, or at least requiring an Optional type.

You can express safety properties such as a sanitized string vs. unsanitized. You can have a Sanitized type that can only be created by calling a sanitize function - which carefully escapes/handles any invalid characters - and then functions that might, say, pass a value into an SQL instruction can be typed to only take Sanitized strings. Now the representation in memory of Strings and Sanitized strings is identical, but by using different types and a certain set of allowed functions on those types, you can encode the invariant that a string cannot be inserted into an SQL query until it has been sanitized. Now your type checker can catch SQL insertion vulnerabilities for you. How's that for a type error?

Re: The advantages of static typing, simply stated

#55

Earlier quoted context omitted.

How can you define throwing an error at runtime instead of compile time an advantage?

I didn't claim it was. That's one of the tradeoffs you make in exchange for a simpler, more forgiving programming model (and the ability to do serious work in a REPL or notebook). However, given that Julia programs can be mostly type inferred before execution, it is possible to check for this kind of error before running a program. There have been some projects to do exactly that [1] [2], and I suspect that once Juli…

You did...

> For example, most of these advantages apply to Julia as well...

and then you listed it as an advantage.

Re: The advantages of static typing, simply stated

#56

It's frustrating that this static vs dynamic 'battle' still goes on. The longer everyone thinks this is actually a problem, the longer we have to wait for innovations to happen. Look at the web in 2016, the technology is a complete disarray. Look at the game industry in 2016, where C++ is thrown around as the the cause and solution to all life's problems. A language created 33 years ago now with no intention of being…

I've dealt with dynamic typing vs static typing A LOT recently. In my experience static typing has a major complication that most programmers don't think about: datastructures. When you are dealing with a list in Python for example, you just throw whatever you are working with into that list. Whereas Java Generics and C++ Templates are very ugly because you can't just say "this is a list of stuff" you have to say "this is a list of a set of lists of ints". Not only is it ugly to write and read, it gets worse whenever you have any errors involving it. Ever seen a C++ compiler error message with templates involved? Other static languages like C don't even really try to deal with datastructures and just leave it up to the programmer to manually access the memory.

Re: The advantages of static typing, simply stated

#57
post #45

> Is it safe to call f(x)? The author asserts that static typing allows the compiler to answer this question, but this only allows the compiler to spot type errors in advance. There are many other kinds of errors that are completely invisible to the compiler. In a dynamically typed language, if I don't spot the error from reading the code, I must wait until runtime/testing to discover the error. This is also true for…

> Personally, type errors haven't been the kind of errors that haunt my dreams.

The point of strongly-typed systems is that you can represent your constraints as types. This takes extra thinking and work, but gives you almost almost unlimited expressive power (ref: agda).

Simple example: meters and feet as different numerical types. When you multiply them, you get a silly unit (foot-meters) that doesn't fit with whatever you wanted (meters^2), and thusly fails compilation.

Re: The advantages of static typing, simply stated

#58

My disagreement starts with the beginning of this: "A large class of errors are caught, earlier in the development process, closer to the location where they are introduced." I would re-state this as: "A large class of errors are introduced, which otherwise would not exist." Consider dealing with JSON in Java. Every element, however deeply nested, needs to be cast, and miscasting leads to endless errors, elsewhere in…

In Haskell you can generate a parser to a typed structure using library combinators and code generation:

    data DataMessage = DataMessage { company :: Text
                                   }
      deriving (Show)
    $(deriveJSON defaultOptions ''DataMessage)

    data DataList = DataList { _data :: [DataMessage]
                             }
      deriving (Show)
    $(deriveJSON defaultOptions { fieldLabelModifier = drop 1 } ''DataList)

Which will fail properly when the data is malformed and in the real-world is even shorter, because you can set your data-type to protocol naming conventions and share them throughout.

Then there isn't much bloat. You wrote your types and generated your way to parse them.

You could test with:

    main = do
        print (encode (DataMessage "some-company"))
        print (decode "{\"company\":\"some-company\"}" :: Maybe DataMessage)

        print (encode (DataList [DataMessage "c1", DataMessage "c2", DataMessage "c3"]))
        print (decode "{\"data\":[{\"company\":\"c1\"},{\"company\":\"c2\"}]}" :: Maybe DataList)

https://gist.github.com/b30f6f09a737dcc980e052b0f3d2a39e

Re: The advantages of static typing, simply stated

#59
post #31

Earlier quoted context omitted.

Have you worked with a public api in the last few years that didn't depend on json?

'Everybody else is doing it' is insufficient justification to keep on doing it. As it happens, I agree that JSON is probably a long-term mistake, although it is definitely an improvement over XML, the previous player in that space.

Every technology is relatively wrong. Time points out where and we fix it (process is repeated again forever).

Re: The advantages of static typing, simply stated

#60
"This is sort of a subtle point: when programming in a static language, you always have a choice about what information you encode in the types, and how you encode things. ... In a static language, you do always have the option of building a less typeful API, where less is enforced by the types, but it is often tempting to spend more time encoding things statically (and then proving things to the typechecker) than would be saved by avoidance of potential future bugs. With experience, you develop a good sense for what is worth tracking statically and what to keep dynamic, but newcomers to static languages can make bad tradeoffs here, which in turn contributes to needless complexity in the language’s library ecosystem. "

This is a very important point that I've rarely seen talked about explicitly though I think all programmers develop a tacit sense of it.

Post reply on HN