Given that the CLR generics implementation supports more features than the JVM's, such as reification, and the JVM's generics are a mere Java "compiler trick", why are higher-kinded types not possible in F# but possible in Scala? Does the CLR generics implementation somehow get in the way of things, whereas the JVM's lack of one allows you to go further than what the designers intended; somewhat like dynamic languages let you do tricks that a strongly-typed compiler would make impossible?
-
11Why did this get closed? I really don't understand the explanation given. I'm supplying facts, asking a question that has a factual answer. Someone seemed to latch onto the word "better" as the question originally was stated, and created an argumentative answer, but that wasn't what the question was about. Do people just look for keywords like "better" and vote to close a priori? – lobsterism Apr 25 '14 at 14:04
2 Answers
In principle, I'm not sure that there's anything preventing F# from including higher kinded types; the CLR doesn't natively support them, so a more indirect compilation strategy would need to be used, but that's the case for Scala on the JVM too. It may or may not be more complicated to do this on top of the CLR's reified generics, but I suspect the reason that F# doesn't include them is more philosophical. While higher kinded types would be nice, F#'s design tends to favor simple features that interoperate well with other .NET languages; higher kinded types would probably require significant effort to add to the language, would complicate type inference and other parts of the language and would definitely not interop well with C# and other .NET languages, so the costs (including opportunity costs) were probably perceived to outweigh the benefits.

- 54,864
- 2
- 91
- 133
-
-
1@lobsterism - I'm not a Scala expert, but since the Java type system doesn't support them, I'm not sure how they could. – kvb Feb 26 '13 at 18:14
Better? Conceivably.
The only difference I'm aware of is a feature of generics called "specialization." And it's available under programmer control in Scala. So... I'd have to say one can make the case that Scala is "better."

- 26,420
- 4
- 61
- 81
-
Scala *is* better, that's what I'm saying, but wondering why it's not possible to so in F# when the CLR has the more powerful type system compared to JVM. – lobsterism Feb 26 '13 at 05:23
-
@lobsterism Java and JVM are not the same thing... That is how scala can have a "stronger" typing system compared to java but both use the same JVM! – korefn Feb 26 '13 at 07:03
-
1In fact, Scala's "specialization" exposes a short coming of the JVM: primitive types are outside of the object hierarchy and generic types are not reified. On-the-other-hand, .NET and F# have `ValueTypes` which are part of the object hierarchy while having all of the performance benefits of the JVM's primitive types. Which means a type like `List
` in .NET (where `Int32` is a .NET `ValueType`) is fully reified and requires no boxing / unboxing to access elements. – Stephen Swensen Feb 26 '13 at 15:30 -
It's easy to dismiss erasure as a bad thing, but it has significant economies. Scala allows us to accept auto-boxing of primitives or specialize, as we the programmer see fit. I consider that an advantage. – Randall Schulz Feb 26 '13 at 15:46
-
@korefn That's correct but it doesn't answer my question; the question was, why is the same trick not possible in F#, especially since the CLR has stronger generics support. – lobsterism Feb 26 '13 at 17:32
-
Why would you ever want to auto-box primitives vs. specialize? The .NET JIT does it on the fly as needed for value types, so there is really negligible space consideration (and for reference types, no specialization is required). – Stephen Swensen Feb 26 '13 at 17:51
-
@StephenSwensen: 'Cause creating all those `.class` files when the overhead of auto-boxing is irrelevant to you application can cause severe bloat to the compiled code of your system. – Randall Schulz Feb 26 '13 at 18:22
-
… I meant to say "irrelevant to the _performance_ of your application..." – Randall Schulz Feb 26 '13 at 18:28
-
3@RandallSchulz ah, I see, that makes sense for Scala precisely because the JVM does not have reified generics so specialization needs to happen in the compiled output of your program, whereas with .NET, it happens on-demand at runtime in the virtual machine itself. – Stephen Swensen Feb 26 '13 at 18:45