News for hackers. Stuff that matters.
Re:Announcing TypeScript 0.9.1 by sdevlin on Tuesday August 06, 2013 @10:56PM (#6170544)
I sometimes find myself torn between dynamic typing (great for rapid prototyping) and static typing (great for tooling and compile-time error checks).

I'm a big fan of TypeScript's approach, which gives you the best of both worlds. Start with fully dynamic code to explore the problem space, then add more static guarantees as you firm up your design.

I haven't had occasion to write a big web app recently, but I'm itching to find one so I can put TypeScript through its paces.

Re:Announcing TypeScript 0.9.1 by marshray on Wednesday August 07, 2013 @01:00AM (#6170944)
Did I just hear someone say they were looking for a web app to write? I think he may even know something about software engineering ...

Don't everybody mob him at once :-)

Re:Announcing TypeScript 0.9.1 by delambo on Wednesday August 07, 2013 @09:56AM (#6172814)
I think the greatest advantage to static typing is the IDE support. Compile-time error checking is overrated, and in my several years of writing JavaScript, I've never seen a bug in production that could have been prevented with static analysis. That might be because I know the language well, but it's not a feature I need.

"What's true of all bugs? They passed a type checker and they passed the tests!" - Rich Hickey

Re:Announcing TypeScript 0.9.1 by sdevlin on Wednesday August 07, 2013 @10:42AM (#6173145)
Your point is well taken, but I think it has value. Compile-time error checking is useful because it gives you flexibility to make changes in a big program. It's kind of like a big suite of automatic tests that make sure all the parts of your program talk to each other correctly.

Does it catch everything? Can you stop thinking critically? No, but it's still nice to have.

This sort of goes hand in hand with IDE tools (e.g. "change method name" sorts of things), so I don't think we necessarily disagree.

> I've never seen a bug in production that could have been prevented with static analysis.

That seems unlikely to me.

I do application penetration testing for a living, and I'll often use static analysis tools in my work. (Both robust, established tools and ad hoc scripts.) For example, check out Brakeman for Rails apps. These tools find actual bugs in actual production software.

Do they find everything? No, you can't rely on them completely. But they're still nice to have.

Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @05:18AM (#6171681)
I was in the same position for literally years regarding static vs dynamic. I've concluded that C gets it spot on i.e. it's mostly static with associated guarantees but you can use a void* if you need it.
Re:Announcing TypeScript 0.9.1 by mahmud on Wednesday August 07, 2013 @05:29AM (#6171715)
Probably the single worst conclusion someone can arrive at regarding typing.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @05:47AM (#6171761)
Rather than slate me, some constructive discussion would be nice. I'll go from my side:

1. Static type compiler verification saves tonnes of problems. Try managing a 2MLOC dynamically typed solution and you'll get what I mean.

2. Static typed languages are way easier to refactor as more metadata is available to the tooling.

3. Static typed code is easier to test. The type contract is available over the boundary between the implementation and the test cases.

4. Defined interfaces without leaky abstractions are easier to produce when you have static types.

5. Static types allow the compiler to infer more information about how to compiler your code resulting in faster, more efficient code and lower memory usage plus you don't have to compile an instance of a function with every possible type consideration at runtime.

6. Statically typed data serialises more reliably i.e. goes over the wire easier without behemoth contracts and parsers at each end.

7. Statically typed languages tend to have better numeric accuracy as differences between decimal, floats and integers are always deterministic and there are defined casts and conversions between each during operations.

I could go on...

Re:Announcing TypeScript 0.9.1 by mahmud on Wednesday August 07, 2013 @06:21AM (#6171828)
Your heart is in the right place, but you're stuck with a bad example still.

C is hardly a good representative for an statically typed language. Read Luca Cardelli's "Typeful Programming"[1] to get an overview of what typed programming is all about, and see for yourself how brain-dead C and C++ are in comparison. After that go to Benjamin Pierce's canon, "Types and Programming Languages".

[1]http://www.daimi.au.dk/~madst/tool/papers/typeful.pdf

Re:Announcing TypeScript 0.9.1 by nly on Wednesday August 07, 2013 @08:12AM (#6172227)
I don't think it's fair to call C++s type system 'brain-dead' when it was developed pragmatically to be largely source compatible with C.

It introduced stronger array types, eliminated Cs automatic void* -> T* conversion and, most importantly, put higher-order functions and types on the table.

It might be a warty syntactical abomination, but it can still hold its own against some of the languages of type extremism.

Re:Announcing TypeScript 0.9.1 by marshray on Wednesday August 07, 2013 @06:37PM (#6176143)
So what would you consider to be the "good representatives for an statically typed language"?
Re:Announcing TypeScript 0.9.1 by mahmud on Wednesday August 07, 2013 @10:24PM (#6177007)
A language with a statically enforced type system? Anything in the ML family for starters, but more popularly, Java, C#, the Pascal family, etc.
Re:Announcing TypeScript 0.9.1 by seanmcdirmid on Wednesday August 07, 2013 @06:45AM (#6171906)
C has very simple types and allows for untyped operations very easily. Say you cast some ptr of type void* to Foo*: at least in Python you would get a dynamic type error if the cast was incorrect; in C you are lucky if your program immediately crashes.

C has some static type safety, and virtually no run-time type safety. Java on the other hand, has a lot of static type safety, is completely memory safe (barring bugs in the implementation), and checks all coercions dynamically. Python has what Java has dynamically, and nothing statically, but it is still 10 times better than what C has!

Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @07:32AM (#6172067)
That's what we have valgrind, test cases and decent software engineers for.

Java don't forget suffers as do other languages from lots of nasty things related to types including invalid casts, null reference exceptions etc. When these go phut in production, you're usually in the same situation.

Re:Announcing TypeScript 0.9.1 by pkinsky on Wednesday August 07, 2013 @10:02PM (#6176938)
Java is not a good example of a statically typed language. Null pointer exceptions just don't happen in idiomatic Scala. For example, let's say I have 3 functions a,b and c, each with signature Int => Option[Int]. Any one of these can return Some[Int] or None.

Options, like all collections, can be chained together as such:

   for {
      x <- a(1)
      y <- b(x)
      z <- c(y)
   } yield z
this expression is of type Option[Int], and will never throw a null pointer exception. (Futures can be chained together with identical syntax)
Re:Announcing TypeScript 0.9.1 by seanmcdirmid on Wednesday August 07, 2013 @11:10PM (#6177151)
> Null pointer exceptions just don't happen in idiomatic Scala.

But match exceptions do.

Anyways, if I had to choose between unsound static type checking and sound dynamic type checking, I would definitely choose the latter.

Re:Announcing TypeScript 0.9.1 by happy_dino on Thursday August 08, 2013 @08:46AM (#6178529)
> But match exceptions do.

I'd call that non-sense.

Re:Announcing TypeScript 0.9.1 by lucian1900 on Wednesday August 07, 2013 @06:35AM (#6171874)
5 is not entirely correct. 6 is not true, serialisation in dynamic languages is correctly done with type definitions. 7 is not true at all, mainstream statically typed languages (like C) much more commonly overflow silently. Look at Python, there's no way to lose precision through arithmetic, as types get promoted on overflow correctly.

In general, C's type system is so weak as to be both useless and a hindrance.

Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @09:12AM (#6172543)
5 is true for the cases where it results in static compiled output which is equivalent of say C's output.

6 you're adding metadata with type definitions. Enough metadata is present in entirely statically typed languages to not do this.

7. How do you know if 1.000000000000000000001 is decimal or float?

It's weak in some places but strong in others. Knowing when to use each case effectively is the art.

Re:Announcing TypeScript 0.9.1 by lucian1900 on Thursday August 08, 2013 @05:08AM (#6177936)
6. It's still necessary (or at least a good idea) to write serialisation models for most statically typed languages as well. the difference is minor

7. It's a float, unless you made it a Decimal explicitly. Arguably that's the wrong default, but almost no languages default to arbitrary precision/size everything.

No, C's type system is totally unsound. It fails to catch very bad errors (pointer-related issues, null-related issues, etc.) and is perfectly happy to implicitly cast left and right. It's little more than a hindrance.

Re:Announcing TypeScript 0.9.1 by michaelwww on Wednesday August 07, 2013 @07:17AM (#6172007)
My worst nightmare would be managing a 2MLOC inherited from a programmer who used void * as a convenience so it's a little surprising to hear this.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @07:29AM (#6172051)
void* is used rarely in practice, apart from perhaps kernel mode or language VM's. I have a 150kloc program in front of me which doesn't have a single void*.
Re:Announcing TypeScript 0.9.1 by michaelwww on Wednesday August 07, 2013 @07:42AM (#6172112)
Great. That makes more sense. I must have misunderstood what you were saying.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @09:05AM (#6172500)
Yes we're not gluttons for punishment believe me :)
Re:Announcing TypeScript 0.9.1 by nly on Wednesday August 07, 2013 @08:23AM (#6172275)
void* is used in practically every C library that offers a callback facility.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @09:04AM (#6172496)
Almost...

You have a typed pointer to a function but that's not a void pointer and the "New C Standard" 6.3.2.3 states that you can't cast a function pointer to a void*.

Re:Announcing TypeScript 0.9.1 by ygra on Wednesday August 07, 2013 @09:21AM (#6172595)
Not for function pointers; for passing data to callbacks that can be any type.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @09:23AM (#6172614)
Can you provide an example? Not 100% sure in which context you are talking about.
Re:Announcing TypeScript 0.9.1 by ygra on Wednesday August 07, 2013 @09:54AM (#6172801)
As an example, SQLite:

    int sqlite3_exec(
      sqlite3*,                                  /* An open database */
      const char *sql,                           /* SQL to be evaluated */
      int (*callback)(void*,int,char**,char**),  /* Callback function */
      void *,                                    /* 1st argument to callback */
      char **errmsg                              /* Error msg written here */
    );
Here the void * is an argument that is passed in the callback as well so that you can update your own struct with the result, for example. Singe the library cannot know anything about your types, void * is the only option.
Re:Announcing TypeScript 0.9.1 by harrytuttle on Wednesday August 07, 2013 @11:52AM (#6173599)
Thanks for the example. Agree entirely then :)