News for hackers. Stuff that matters.
Re:The Java type system is broken by mcculley on Thursday July 19, 2018 @10:38AM (#17566688)
The mess that is the current Java type system makes me skeptical when proponents of languages like Go say not to worry, that generics can be added later.

If Java had generics in the beginning, it would look a lot different and the type system would be more powerful and safe.

Re:The Java type system is broken by monocasa on Thursday July 19, 2018 @11:09AM (#17567009)
Unlike Java, Go doesn't have to worry about bytecode backwards compatibility. That was the main issue that had them reaching for type erasure.

Take a look at C# which added it without many issues (because they were willing to break back compat of their bytecode). Go fits this model a whole lot better.

Re:The Java type system is broken by pjmlp on Thursday July 19, 2018 @11:52AM (#17567447)
The missing part every time this story gets told, is that Microsoft already had a generics implementation ongoing being lead by Don Syme of F# fame.

They just decided to release 1.0 without generics, instead of waiting for it to be 100% ready.

As Don clearly describes on his blog.

Re:The Java type system is broken by monocasa on Thursday July 19, 2018 @11:58AM (#17567506)
Sure, totally.

In fact, I'm a big fan of how Microsoft does versioning, supporting previous versions, but releasing major versions with backwards incompatible changes. You see this in DirectX, and IMO, that's why we're talking about DirectX and Vulkan, rather than AZDO OpenGL these days. It really gives you an opportunity to clean up the cruft.

Re:The Java type system is broken by pjmlp on Thursday July 19, 2018 @01:05PM (#17568350)
Yeah, although I think they missed an opportunity to move beyond "only C goes" mindset at Khronos.
Re:The Java type system is broken by majewsky on Thursday July 19, 2018 @03:32PM (#17569547)
Is there any language besides C that's not a complete pita to generate FFI bindings for?
Re:The Java type system is broken by pjmlp on Thursday July 19, 2018 @04:51PM (#17570154)
There are many ways to define APIs, we don't need to struggle ourselves only to C.

Metal uses Objective-C/Swift, with shaders in C++14. The Objective-C runtime can be used as FFI.

DirectX, uses COM with HLSL (a C++ subset). Likewise any COM or .NET (via RCW) aware language can talk to it.

One of the reasons OpenCL lost to CUDA was being stuck with C, while CUDA offered C, C++, Fortran and any additional language that could target PTX.

Which was what eventually made them come up with SPIR and later SPIR-V.

NVN and LibCGNM are also based on C++ and C++ inspired shader languages.

All modern OO, with nice SDKs that handle font, texture, materials, maths, GPGPU debugging.

Re:The Java type system is broken by bunderbunder on Thursday July 19, 2018 @04:40PM (#17570060)
fortran?
Re:The Java type system is broken by alexeiz on Thursday July 19, 2018 @01:19PM (#17568472)
> Take a look at C# which added it without many issues

I wouldn't say "without many issues." It took a major surgery in the CLR codebase to add generics. The difference between CLR 1.1 without generics and CLR 2.0 with generics was huge. No area of code was untouched.

Re:The Java type system is broken by mcculley on Thursday July 26, 2018 @05:19AM (#17616112)
Go doesn’t have an ABI? Does every library ship only as source code? I didn’t know that.

I was thinking of more fundamental changes to Java idiom and the standard library had generics been part of the design (e.g., how arrays work).

Re:The Java type system is broken by enitihas on Thursday July 19, 2018 @11:17AM (#17567102)
Did C# really break backwards compatibility of their bytecode? What I understand is that C# generic and non generic APIs are completely different, and are not inter operable.
Re:The Java type system is broken by bunderbunder on Thursday July 19, 2018 @12:47PM (#17568099)
To the first - yes, the version of .NET that introduced generics broke bytecode backward compatibility. Stuff compiled for .NET 1.0 will not run on the CLR for 2.0 and later. In practice, this wasn't a big deal; maintainers just shipped two different versions of their packages, and you'd download the one you wanted.

To the 2nd - The concrete types are separate, but they fit into a common interface hierarchy, so, at a source code level, they're plenty interoperable. For example, the (non-generic) IEnumerable interface has a Cast<T>() extension method that will convert it to a (generic) IEnumerable<T>. IEnumerable<T>, for its part, simply inherits IEnumerable.

In general, I actually like using those conversion methods for downcasting, because it gives a clearer indication of when I'm in a danger zone - for example, converting a non-generic list to a generic list might fail if the non-generic list contains a mix of different types of object. Java's situation feels less predictable to me, as this article illustrates fairly well. A few keystrokes for the sake of safer code is a dandy tradeoff in my book.

Re:The Java type system is broken by teh_klev on Thursday July 19, 2018 @04:29PM (#17569963)
> Stuff compiled for .NET 1.0 will not run on the CLR for 2.0 and later

That's not true. (Mass shared hoster kinda guy here) when we pushed a bunch of customers code compiled for .NET 1.0/1.1 over to hosting environments with only CLR 2 available, their code ran just fine. These were .NET 1.0/1.1 assemblies.

There are some edge cases where stuff could break, say when doing reflection or emitting your own IL, but that for most LOB web hosted apps was pretty rare.

I did in fact marvel how backwards compatible CLR 2 was when it first shipped.

Re:The Java type system is broken by mohaine on Thursday July 19, 2018 @11:42AM (#17567348)
Not sure on the first part but the second part is true.

When there is a C# class that both has and doesn't have type information, they are actually completely different classes that just happen to share a base name.

This really made it painful when they first added generics. If you used generic containers but wanted to call into a library that predated it you hand to transform your data in/out of all the calls to the library. In Java land you didn't have to do anything other then a blind cast on the out side which doesn't even have a runtime penalty.

Long term the C# way was probably better but then they didn't have nearly as robust of a library ecosystem when they added generics.

Re:The Java type system is broken by kjeetgill on Thursday July 19, 2018 @11:31AM (#17567232)
Yea from the wording I think they mean you can either break backwards compatibility or "fork the world".

In Java's case, old bytecode using Map would correctly work with change to Map <K, V>. C# introduced a new set of collections I believe.

Re:The Java type system is broken by monocasa on Thursday July 19, 2018 @11:54AM (#17567470)
Yeah, IIRC they added the "constrainted." instruction prefix. It's a prefix to the callvirt instruction that constrains the the underlying call to the specified type, allowing runtime generic type decisions/exceptions rather than only compile time decisions like Java.
Re:The Java type system is broken by mnd999 on Thursday July 19, 2018 @03:19PM (#17569450)
Except they didn’t - they introduced annotations at the same time and bumped the class file version for that.
Re:The Java type system is broken by jeremyjh on Thursday July 19, 2018 @07:08PM (#17571142)
Changing the class file doesn't necessarily break backwards compatibility. Are you saying a 1.4 JVM couldn't run 1.3 bytecode?
Re:The Java type system is broken by pron on Thursday July 19, 2018 @12:37PM (#17567989)
> added it without many issues

Except for the fact that all CLR languages must adopt the same variance model, enforced by the runtime (or choose not to interoperate well).

Re:The Java type system is broken by monocasa on Thursday July 19, 2018 @12:42PM (#17568053)
Sure, but in context Go doesn't have that issue. Go's compiler only supports Go in a more or less statically linked binary.
Re:The Java type system is broken by pron on Friday July 20, 2018 @01:45PM (#17577192)
Go doesn't have this issue because there aren't other languages running on that runtime. If there were (and you wanted an interop of those languages with Go so that you could use Go libraries), then you'd have this issue regardless of the fact that the runtime is linked with the image.
Re:The Java type system is broken by kelnos on Thursday July 19, 2018 @08:17PM (#17571480)
I was reading something a while ago (can't find it now, unfortunately) that suggested that the use of type erasure for generics actually opens up the JVM to be able to do some useful things that (for example) the CLR can't do. One thing I recall was that the Scala developers gave up on their CLR/.NET backend.

I also read, more generally, that adding reified generics to the JVM might make it hard or impossible to implement some dynamic languages on top of the JVM.

So yes, erasure has its drawbacks, but it's not all bad.

Re:The Java type system is broken by hyperpape on Friday July 20, 2018 @07:26AM (#17574085)
Re:The Java type system is broken by chrisseaton on Thursday July 19, 2018 @09:06PM (#17571700)
Yes I've heard this from multiple people targeting new languages to the JVM.

Maybe it's why the JVM has a larger ecosystem of new languages?

Re:The Java type system is broken by piinbinary on Thursday July 19, 2018 @11:44AM (#17567370)
Since there is no subclassing [0] in Go, many of those cases are not expressible in Go.

[0] Interfaces can contain other interfaces, but I think that is still different because you can't do

    var a []subinterface = []superinterface{...}
Re:The Java type system is broken by cmiles74 on Thursday July 19, 2018 @11:06AM (#17566973)
I agree that generics should probably be a strong consideration right from the beginning. I disagree that this would mean that it would necessarily be done correctly or that it's impossible to add it later. That was the case with .Net and C#, it wasn't until the 2.0 release that generics were added. [0]

[0]: http://mattwarren.org/2018/03/02/How-generics-were-added-to-...

Re:The Java type system is broken by kjeetgill on Thursday July 19, 2018 @11:01AM (#17566905)
Interesting. How do you think it would be different? I ask because the go-to is usually genetics erasure. Do you think more could or would have been different?

For what it's worth, Brian Goetz doesn't see type-erasure as a such a failure and defends the design choice[0].

[0]:https://m.youtube.com/watch?v=TkpcuL1t1lY

Re:The Java type system is broken by hota_mazi on Thursday July 19, 2018 @02:05PM (#17568871)
Actually, most people who actually understand in depth the subject and the trade offs of erasure versus reification squarely side on the erasure side.

Erasure is the safest way to implement generics for a long list of reasons. Reification comes with a lot of downsides, which is why most languages that support parametric polymorphism use erasure.

Re:The Java type system is broken by kjeetgill on Thursday July 19, 2018 @04:09PM (#17569813)
Interesting, do you have any resources? What people?

Aside from the video I posted from Brian Goetz most of what I've read is just comments about how much people like reification in C# and how annoying those edge cases in Java are (i.e. stories from practitioners not language designers.)

I'd love to read more.

Re:The Java type system is broken by hota_mazi on Thursday July 19, 2018 @04:25PM (#17569928)
There's plenty of material out there but I'll summarize:

1. Erasure keeps you honest and prevents you from second guessing the compiler by limiting the introspection you can perform on your code.

2. Reification puts up a very high barrier to interop. Erasure systems are much more welcoming to implementing multiple languages and multiple type systems on top of them. For example, the scala.net project was abandoned because it was impossible to represent Scala's type system on .net's reified platform.

3. Reification imposes overhead because of the multitude of extra runtime checks that need to be performed.

4. Most the benefits you get from reification can be emulated on erased systems (some are admittedly more hackish on an erased runtime, but they should be rare).

Re:The Java type system is broken by phillipcarter on Thursday July 19, 2018 @08:22PM (#17571499)
I suppose if implemented flawlessly, this would be true:

> Erasure keeps you honest and prevents you from second guessing the compiler by limiting the introspection you can perform on your code

But the title of the article under discussion is called "The Java type system is broken", after all :)

Re:The Java type system is broken by mcculley on Thursday July 19, 2018 @04:02PM (#17569765)
I have not watched the video. All of the defenses of type erasure I have read boiled down to backwards compatibility. If generics had been there from the start, the JVM and the compiler could do more with them, eliminating casts and allowing things like List<int>, getting rid of arrays entirely as a special case and allowing the compiler and JVM to optimize accordingly.
Re:The Java type system is broken by hota_mazi on Friday July 20, 2018 @02:11PM (#17577399)
That's incorrect.

It would have been possible to implement reified generics while preserving backward compatibility, Neal Gafter had actually a proposal to do just that.

In the end, erasure won simply because it's the superior solution.

Re:The Java type system is broken by mcculley on Friday July 20, 2018 @09:26PM (#17580236)
Erasure is superior to a system that would be typesafe? Please elaborate.
Re:The Java type system is broken by hota_mazi on Saturday July 21, 2018 @05:37AM (#17581427)
Erasure is just as type safe as reification.

Erasure gives you a little less flexibility to express certain constructs than reification allows, but from a type safety standpoint, the two approaches are equivalent.

If you want widen the meaning of "type safe" a bit, I would argue that reification is "less" type safe in the sense that it allows you to perform additional runtime type checks / type casts, which basically means you are second guessing the compiler and invalidating all the type soundness that it has provided you by accepting to compile your code.

Re:The Java type system is broken by mcculley on Saturday July 21, 2018 @10:18PM (#17584942)
It would not have allowed one to seal the loopholes demonstrated in the article?
Re:The Java type system is broken by eeperson on Saturday July 21, 2018 @03:49AM (#17581135)
How is reification any more type safe than erasure?
Re:The Java type system is broken by mcculley on Saturday July 21, 2018 @10:18PM (#17584944)
Would it not have allowed one to seal the loopholes demonstrated in the article?
Re:The Java type system is broken by MaxBarraclough on Thursday July 19, 2018 @11:41AM (#17567345)
> If Java had generics in the beginning, it would look a lot different and the type system would be more powerful and safe.

And conceivably a good deal faster, too. My understanding is that the JVM is required to constantly perform runtime type checks.

Re:The Java type system is broken by icedchai on Thursday July 19, 2018 @03:27PM (#17569506)
Good deal faster? I seriously doubt it. My understanding are generics are purely a compile time feature and does not effect run time.

I remember Java before generics: tons of casting from Object. The compiler is doing the same thing.

Re:The Java type system is broken by mcculley on Thursday July 19, 2018 @04:10PM (#17569817)
Those casts are still there. The compiler puts them in and the JVM has to execute them.
Re:The Java type system is broken by icedchai on Thursday July 19, 2018 @04:15PM (#17569843)
Yes. And my point is they'd be there anyway, with or without generics. It's not "generics" making the JVM any slower.
Re:The Java type system is broken by mcculley on Thursday July 19, 2018 @04:26PM (#17569932)
If the types were reified, the compiler and the JVM would know what type was there. No cast would be needed.
Re:The Java type system is broken by MaxBarraclough on Friday July 20, 2018 @04:39AM (#17573390)
> they'd be there anyway

No. That's the point.

Re:The Java type system is broken by monocasa on Thursday July 19, 2018 @12:44PM (#17568068)
Yeah, it either has to cast from Object all the time, or spend JIT compiler time and memory figuring out if it can elide those checks (probably with exception code for when it guesses wrong).
Re:The Java type system is broken by icedchai on Thursday July 19, 2018 @03:28PM (#17569511)
Without generics, the developer would have to cast from Object anyway.
Re:The Java type system is broken by lmm on Thursday July 19, 2018 @03:48PM (#17569662)
The point is that if Java had had generics in the beginning, they could (perhaps) have been integrated into the language in a way that obviated those casts entirely.
Re:The Java type system is broken by leshow on Thursday July 19, 2018 @03:00PM (#17569323)
and pointer chasing too right? unless the runtime can optimize, every generic sits behind a pointer.
Re:The Java type system is broken by sqrt17 on Thursday July 19, 2018 @06:38PM (#17570956)
Go's type system with limited polymorphism looks to me like someone took the (horrible) collections from Java 1.2 and applied a fix that solves 85% of the problem. Compared to the 95% solution we get with Java, it's not as refined but also plenty good enough in most cases where concrete code deals with concrete problems (i.e. not framework-level code where you'd curse Java for lacking Scala's types-and-implicit-driven compile time contorsions).
Re:The Java type system is broken by microtherion on Thursday July 19, 2018 @07:11PM (#17571166)
My impression (having not tried this part of the language much) is that Swift underwent similar growing pains with their generics (but with the benefit of being willing to break backward compatibility). After 40 years or so of generics, it seems language designers STILL think "all these languages before me are overcomplicating the matter, I bet I can do this in a much simpler way".
Re:The Java type system is broken by slavapestov on Thursday July 19, 2018 @07:20PM (#17571212)
The implementation of generics has undergone major changes and new features were added, but the conceptual model is pretty much what it was in Swift 1. This was one part of the language that they almost got right from the start.
Re:The Java type system is broken by tabtab on Thursday July 19, 2018 @11:57AM (#17567504)
Just use dynamic or better yet type-tag-free languages. Types are hard to get right and hard to reason about. Sure, there are some Sheldons out there who master it even under pasta applications, but most of us are not Sheldons. C# also bleeped up types by adding nullable types, making reflection into a scavenger hunt.

For dynamic or tag-free languages, type indicators could be used to parse-check scalar (base) values to see if they are interpretable as the intended type:

   function foo(int a, date b) {...}
This would be equivalent to:

   function foo(a, b) {
     if (! parsableAsInt(a)) throwTypeError(...);
     if (! parsableAsDate(b)) throwTypeError(...);
   }
And don't overload operators, such as how some languages use "+" to mean both arithmetic addition and string concatenation. Use a different symbol for concatenation like PHP does.
Re:The Java type system is broken by twblalock on Thursday July 19, 2018 @02:14PM (#17568953)
I prefer my type errors at compile time.

You have to reason about types in any language because passing the wrong type to a function can result in errors. You have to do all of that mental work by yourself in a dynamic language because there isn't a system to do it for you.

Re:The Java type system is broken by tabtab on Thursday July 19, 2018 @08:54PM (#17571648)
There are tools roughly similar to C's "lint" that can warn you about suspicious-looking code. While compile-time checking can be nice, compiler-oriented languages often result in more verbose code. Verbosity introduces errors also. But the benefit weighing also depends on the kind of applications. I didn't mean to trigger a "holy war" of static versus dynamic languages.
Re:The Java type system is broken by runeks on Sunday July 22, 2018 @09:56AM (#17586758)
> Verbosity introduces errors also.

The purpose of types is to introduce errors in the first place - at compile time, if you messed up.

Wrong code causes errors regardless. Annotating with types just moves this event from runtime to compile time.

Re:The Java type system is broken by tabtab on Monday July 23, 2018 @11:18AM (#17593319)
I understand that, but I'm not sure the benefits exceed the drawbacks. In other words, verbosity introduces errors by making the code harder/longer to read. The type-related verbosity may reduce errors, but perhaps not enough to counter those caused by verbosity. In my experience, it's roughly a wash, but depends on a lot of other things, like frameworks used, skill of developers, QA techniques, etc.
Re:The Java type system is broken by rs86 on Thursday July 19, 2018 @07:09PM (#17571154)
The thing I most resent is that dynamic languages are so widespread. It is so pleasurable to write haskell or ocaml!
Re:The Java type system is broken by recursive on Thursday July 19, 2018 @12:11PM (#17567678)
> C# also bleeped up types by adding nullable types, making reflection into a scavenger hunt.

Having used reflection in C# with both nullable types and non-, I'm curious what you mean by "scavenger hunt".

Re:The Java type system is broken by erik_seaberg on Thursday July 19, 2018 @12:58PM (#17568235)
PHP only got this halfway right because it borrowed "." from Perl. It should have also borrowed "eq" instead of making "==" unusable.
Re:The Java type system is broken by tdsamardzhiev on Thursday July 19, 2018 @03:46PM (#17569645)
True, but I think that for bigger applications static typing is better even if you don't get it quite right.
Re:The Java type system is broken by AnimalMuppet on Thursday July 19, 2018 @02:36PM (#17569136)
That would solve the problem with adding generics, yes. It would also destroy the thing people appreciate about static typing - that you get told at compile time when you've done something wrong.
Re:The Java type system is broken by dep_b on Thursday July 19, 2018 @12:46PM (#17568095)
String concatenation got a lot nicer in C# lately but the old method is still there