News for hackers. Stuff that matters.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by munificent on Thursday June 20, 2019 @05:07PM (#20237407)
Dart isn't optionally typed any more. It's now a fully statically typed language, that also has a special "dynamic" type. That puts it in the same boat as C# and Scala, among others.

Optional or gradual typing does seem like an obvious brilliant idea from the outside. Start out dynamic when the program is small, layer in types when it grows to the point where you need them. Capture the union of both dynamically typed and statically typed users. Everyone wins!

In practice, we found ourselves in an uncanny valley where we were too typed for the dynamic typing folks, and too unsafe for the static ones. We couldn't deliver the user experience either camp expected. We learned, the hard way, that a statically typed language is not simply a dynamically typed language plus some type annotations. Everything about how you use the language is different.

---

The way you design APIs is different

Python's tuple type has a subscript operator to return an element at the given index. That's a perfectly reasonable, simple, clean API in a dynamically typed language. If you want to have statically typed tuples, that API doesn't even make sense:

   t = (1, True, "three")
   x = t[datetime.datetime.today().weekday() % 3]
What is the static type of x?

Another example: Python's list type has a sort() method. It takes an optional "key" argument that is a callback that converts each value to a key of some time and then sorts using those projected keys. If you pass a key function, then sort() needs to be a generic function that takes a type parameter for the return type of the key function, like:

    sort<R>(key: (T -> R))
But if you don't pass the key function, the R type argument is meaningless. Should it be a generic method or not?

An even gnarlier question is "What kinds of lists can be sorted at all?" The sort() method works by calling "<" on pairs of elements. Not all types support that operation. Of those that do, not all of them accept their own type as the right-hand operand. How do you design the list class and the sort() method such that you ensure you won't get a type error when you call sort()?

To handle this kind of stuff, the "best practices" for your API design effectively become "the way you would design it in a fully statically-typed language". But those restrictions are one of the main reasons people like dynamic languages.

You can mitigate some of this with very sophisticated type system features. Basically design a type system expressive enough to support all of the patterns people like in dynamically typed languages. That's the approach TypeScript takes. But one of the main complaints with static type systems is that they are too complex for humans to understand and too slow to execute.

This makes that even worse. TypeScript's type system is very complex and type-checking performance is a constant challenge. In order to let you write "dynamic style" code, TypeScript effectively makes you pay for a super-static type system.

---

User expectations are bimodal

Once you ask people to design their APIs such that they can be statically typed and then let them start writing type annotations, we observed that they very quickly flipped a mental bit and expected the full static typing experience. They expected real static safety where certain errors were proven to be absent. They expected the performance of a statically-typed "native" language.

But most optional or gradually typed languages are unsound in order to allow typed and untyped code to intermingle. That means type errors can still sneak through and bite you at runtime. It means you get none of the compile-time performance benefits of static types. Sorbet asks you to write your code with all of the discipline, restrictions, and cognitive effort of a statically-typed language. In return, it gives you the runtime performance of... Ruby.

Worse, actually, because it is checking your type annotations dynamically at runtime. It basically turns your type annotations into assertions. So you get even more potential runtime failures.

This was how Dart 1.0 worked. I used to joke that we gave you the best of both worlds: the brevity of Java and the speed of JavaScript. And then I cried a little.

---

This sounds like I'm criticizing this approach to languages. I actually think TypeScript, Flow, Sorbet, and others are a really smart solution to a very challenging problem. If you have a very large corpus of dynamically-typed code that you want to keep extracting value out of, they give you a way to do that while getting some of the benefits of types. If I was sitting on a giant pile of JS or Ruby that I had no plans to rewrite, I would absolutely use one of these tools.

But for new development, I think you're much better off choosing a modern statically typed language if you think there's a chance your program will grow to some decent size. By that, I mean C#, Go, Swift, Dart, Kotlin, etc. Type inference gives you most of the brevity of dynamic types and you'll get all the safety and performance you want in return for your effort to type your code.

If you're going to do the work to make your code typable, you should get as much mileage out of it as you can. So far, no one I know has figured out how to do that with an optionally or gradually typed language.

---

This is, of course, just my personal preference. And I'm biased because I've already walk the long painful educational road to understand static types. One of the real large benefits of dynamic types is there is much less to learn before you can start writing real code. For new users, hobbyists, or people where programming isn't their main gig, this is huge. I love that dynamically typed languages exist and can serve those people.

But my experience is that if you're a full time professional software engineer writing real production code eight hours a day, it's worth it to get comfortable with static typing and use it. The fact that basically every large software shop that had a big investment in dynamically typed languages is trying to layer static typing on now probably tells us something. Google with Closure Compiler and Dart. Microsoft with VB.Net, TypeScript, and Pyright. Facebook with Hack and Flow. Apple with Swift.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by abhorrence on Thursday June 20, 2019 @05:17PM (#20237481)
TypeScript actually handles the first example quite well. If you simply have a heterogenous array type, the type of its members will be the union type of `number | boolean | string`.

If you’ve used a typed tuple, then the type after access is based on what TypeScript statically knows. So array[0] would be number, but array[random() % 3] would be the union type.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by munificent on Thursday June 20, 2019 @07:11PM (#20238355)
That's pretty nice, but it's also a large amount type system machinery for what is in Python an idiot-simple API. Imagine you have:

    array[0] + 2
And you change it to:

    array[random() % 3] + 2
Now you get a type error on "+". Think about the level of type system sophistication you need to have as a user to understand why that change caused that error.

In some senses this is even worse than programming in either a dynamically typed language or a statically typed one. You still get the performance and unsoundness of a dynamically typed language. But you also get the type errors and cognitive load of a typed language, with a type system that is much more complex than a typical typed language.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by seanmcdirmid on Thursday June 20, 2019 @08:29PM (#20238806)
The better feedback loop actually makes it worth it. Yes, we don't get soundness and performance when programming in TypeScript, but we do get better feedback in the editor along with great escape hatches for customizing types as needed. I don't know if I ever can go back to a sound statically typed language again, actually, TypeScript is just way too useful.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by Zarel on Friday June 21, 2019 @03:55AM (#20240734)
I feel exactly the same way. I thought I liked static typing more than dynamic typing until I tried TypeScript, but TypeScript feels like by far the best of both worlds.

I especially feel like null checking is impossible to live without, and I'm going to strongly prefer TypeScript over Go or Dart from munificent's list until they add null checking.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by munificent on Monday June 24, 2019 @05:11PM (#20268526)
We're working on static null checking in Dart now. Unlike TypeScript's, Dart's approach to null checking will be sound.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by muglug on Thursday June 20, 2019 @11:07PM (#20239550)
> But for new development, I think you're much better off choosing a modern statically typed language if you think there's a chance your program will grow to some decent size.

> By that, I mean C#, Go, Swift, Dart, Kotlin, etc.

I think all of those require compilation. While I loved writing C# in a previous job, the compilation step added some small amount of friction to regular web development.

Though working with PHP requires more thought for the big-picture stuff (no PHP ORM can touch what C# offers) I find it easier to get into a state of flow when developing new features. Once I've completed a given task I can run a static analysis tool (I've made one at Vimeo, but there are others to choose from) that can automatically add most of the types I neglected to add, and can suggest more.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by dharmatech on Thursday June 20, 2019 @06:39PM (#20238103)
> Dart isn't optionally typed any more.

Gilad was (religiously) in favor of optional types. Did he come around to agreeing with the direction that Dart has taken (i.e. full static types)?

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by self on Thursday June 20, 2019 @06:46PM (#20238158)
He left Google a while ago.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by munificent on Thursday June 20, 2019 @07:12PM (#20238363)
Lars, Kasper, and Gilad have all left the project.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by debug-desperado on Thursday June 20, 2019 @07:48PM (#20238614)
Interesting. Looks like Kasper and Lund are on a mission to fix the IoT mess and "democratize embedded development". I'm looking forward to their product launch.
Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by derefr on Thursday June 20, 2019 @05:40PM (#20237678)
> What is the static type of x?

an anonymous (Integer | Bool | String) sum-type, of course.

Re:Open-sourcing Sorbet: a fast, powerful type checker for Ruby by edflsafoiewq on Friday June 21, 2019 @01:08AM (#20240012)
Not a sum type, since if all the components were strings you'd have (String | String | String) = String. The sum type would be 3 × String instead.