Hacker News new | ask | show | jobs
by kagakuninja 888 days ago
Type erasure is an interesting discussion, because functional programmers love type erasure. Consider the function:

    def foo[T](t: T): T    
If the function is truly generic, then we know nothing about the implementation details of T. The only meaningful implementation of foo is identity:

    def foo[T](t: T): T = t
From a FP perspective, T should be erased, and the programmer should not cheat and use reflection. The type signature should tell you much about what the function does.

I've used Scala professionally for the last 8 years. The tooling is fine, assuming you avoid exotic libraries like Shapeless. Compile times are fast enough that I don't think about it much, using IntelliJ incremental compilation. A full recompile of a micro service might take 30 seconds, whatever it is is not a big deal IMO.

> opening projects in an IDE like IDEA could take up to 30 min on high end workstations

This is insanely wrong. Maybe you are operating on experiences from 10 years ago? I have a new M2 MacBook Pro, and opening projects is quite fast. The first time you do it, it will resolve the SBT dependencies and index files. That can be done in the background, and is probably less than a minute for a typical project. Even my previous laptop could open projects quickly despite being 3 years old.

> the Scala data structures have been at least an order of magnitude slower than the JVM native ones

They are slower, but not an order of magnitude slower. Perhaps you are remembering the infamous email from the Yammer CTO that got leaked, but that was a long time ago, and the compiler and libraries have improved greatly since then.

But yes, in performance critical code, you can just switch to an imperative style and use Java collections.

I don't have time to explain the benefits of Scala features, but I'll just point out that other than implicits, many ML inspired features of Scala have made their way into modern languages, including Java, C#, Rust and Swift. Scala didn't invent those ideas, but repackaged them in a novel way.

1 comments

IMHO the keyword is 'microservice': Compile times of 30sec for a microservice are not acceptable for me, btw. The projects I am referring to were bigger, and the time to compile/open projects in IDEA where _not_ acceptable at all. (Btw.: I am speaking about Scala experts, not some new grads which didn't know what they were doing.)

Concerning your type erasure example, the id function obviously doesn't need to know anything about types. In the real world, types have traits/interfaces/expected attributes etc. and type erasure prevents the compiler to verify this when using binary dependencies, which is obviously _not_ what one wants in a statically compiled language.

I just compiled our oldest, largest micro service, it was 17 seconds. This is a full recompile, which I rarely need to do, because IntelliJ has incremental compilation. If you have a giant mono-repo, then compile time becomes important.

I can only guess what these "Scala experts" were doing, there are certain language features that can slow your compile times, and libraries like Shapeless that leverage those features in amazing ways, but can kill your compile times.

Scala is a toolkit that enables amazing things, but you have to understand the trade-offs, both in terms of compile time, and understandability of the code.

>In the real world, types have traits/interfaces/expected attributes etc. and type erasure prevents the compiler to verify this when using binary dependencies, which is obviously _not_ what one wants in a statically compiled language.

Scala has ways of recovering the information, such as using context-bounds and type manifests. Scala can actually verify an amazing amount of constraints at compile time, but the more exotic tricks will increase compile time.

What you are complaining about are foot-guns that rarely happen in my 20 years experience with JVM languages (I think generics were added in Java 5, but still...)