| ▲ | torginus 8 hours ago |
| I know its a faux pas in the Java world to acknowledge the existence of .NET, but how does this differ from .NET structs? Value types, generic specialization, boxing - a quick skim makes it looks like they picked the same choices. |
|
| ▲ | DarkNova6 5 hours ago | parent | next [-] |
| C# actually has a fair amount of gotchas and Java aims to make these explicit. So where C# mostly copied C from a low level perspeCtive, the Java guys approached this high level and analyzed in detail which constraints give you what kind of benefit. So where in other languages, the struct/class taxonomy is binary, Java allows more granular control, reflection the semantics of the underlying domain. Snd as it turns out, structs have a wide range of footguns, especially in a parallel context. |
| |
| ▲ | jaen 3 hours ago | parent [-] | | Could you actually explain/exemplify any of the gotchas and what's been made better (or is this just handwaving)? | | |
| ▲ | cogman10 an hour ago | parent | next [-] | | Part of the reason Java hasn't reified generics is because C# did and it was a real big headache that also limited non-C# languages on the C# runtime (CLI?). Everything had to be recompiled to work with newer C# runtimes. While it's pretty easy to run a bunch of language on the JVM (Javascript, python, ruby, clojure) doing the same for C# is somewhat a nightmare, particularly for non-type aware languages. For example, Imagine you have an api like `void do(List<Foo> foos)`. In the erasure environment of the JVM that looks like `void do(List foos)`. From python it's pretty easy to call with a `foos = [Foo()]`. But not so much if your python implementation needs to figure out how and if it can coarse it's `List` type into a `List<Foo>` type. | | |
| ▲ | kuhsaft an hour ago | parent [-] | | I don’t think that’s the case. You can absolutely implement a type-erased language on top of the CLR. Your language will just have the same constraints of a type-erased language like Java. Having reified generics in the CLR just lets you store more type information. There isn’t much of a trade off for CLR end-users. Compare this to the constraints and workarounds that Kotlin and Scala have due to type-erasure on the JVM. | | |
| ▲ | cogman10 41 minutes ago | parent [-] | | You CAN do it, but it's much more difficult. And as far as I'm aware, both kotlin and Scala don't really suffer due to type erasure. | | |
| ▲ | kuhsaft 28 minutes ago | parent [-] | | I mean, the language is what it is. But, it definitely constrains the language developers. Especially when considering interop with other JVM languages. That being said, it is easier to write a language on top of the JVM with good interop, since there are less ways to implement features. Essentially, your language has to interop with Java. And it is harder to have good interop between CLR languages because there are more ways to implement features. Essentially, your language has to interop with C#. |
|
|
| |
| ▲ | kuhsaft 2 hours ago | parent | prev | next [-] | | The gotcha is the potential boxing of structs onto the heap, but that can be avoided using `ref struct`s. https://news.ycombinator.com/item?id=48599273 | |
| ▲ | bbg2401 2 hours ago | parent | prev [-] | | Why would you presume the parent is "just handwaving"? It's odd how people in the .NET community struggle to earnestly engage in conversation with Java folk. The reverse isn't true. |
|
|
|
| ▲ | _old_dude_ 8 hours ago | parent | prev | next [-] |
| The article has a section about that. For me, a struct in C/C# can be modified and is passed by copy while a value class can not be modified and is passed by value. I do not think you can do stack allocation in Java. |
| |
| ▲ | kuhsaft 2 hours ago | parent | next [-] | | Like @layer8 said, pass by copy and pass by value are the same. C# copies C++ behavior where you can pass a struct by value or reference, and you can mark the parameter as readonly. C# also has in/out parameters. Essentially, you can program in C# exactly like you would in C++. https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... The footgun with C# structs are that you can accidentally box them onto the heap. To avoid that you can define `ref struct`s that cannot be boxed. `ref struct`s follow the C# disposable pattern. https://learn.microsoft.com/en-us/dotnet/csharp/language-ref... | |
| ▲ | layer8 8 hours ago | parent | prev [-] | | I don’t see a difference between pass by copy and pass by value. The mutability difference is that part of a struct can be modified in place, which value classes can’t: the value of a complete value-class variable (or array slot) can only be modified (reassigned) as a whole. This is presumably because object references to value-class objects can be created, and those objects should be immutable so their identity doesn’t matter. | | |
| ▲ | _old_dude_ 8 hours ago | parent | next [-] | | I think pass by copy is a consequence of being modifiable. The other solution is to stack allocate and pass a pointer but as i said, unlike in C#, i do not think it's possible to do that in Java. In Go, you can stack allocate but when you send a pointer (that escapes), the compiler will heap allocate the object. | | |
| ▲ | layer8 8 hours ago | parent [-] | | My point is that pass by copy and pass by value do the same thing, they copy the value representation. In other words, pass by copy means exactly pass by value. | | |
| ▲ | gf000 7 hours ago | parent | next [-] | | Actually, Java only has pass-by-value, even for reference types. (The same way as C does). People really misuse/misunderstand this term: Java objects are passed by their pointers ("references") being copied. The alternative is pass by reference, which is done by e.g. c++, rust, who actually have references (Java doesn't). A good litmus test is whether you can write a swap method that actually changes your local variables. | | | |
| ▲ | _old_dude_ 7 hours ago | parent | prev [-] | | For me, the difference is that if methods are inlined, the compiler is still required to do a copy for structs but not for value classes. I do not know how this is called. |
|
| |
| ▲ | gf000 8 hours ago | parent | prev [-] | | I think that's mostly a semantic difference - Java avoided the problem of strange lifetimes, captures, tearing by fixing the semantics as immutable value objects, while C# has to deal with these issues. But under the hood it can (and will) do a modification in place. |
|
|
|
| ▲ | rf15 8 hours ago | parent | prev [-] |
| Functionally they don't - java is just catching up with (by now) ancient practice. The false dichotomy of > A struct in C# has identity and mutation, so the semantics of copying on assignment or passing have to be precisely defined, which gives a heavier model for the programmer and less freedom for the runtime. Doesn't really match with what they're describing. While yes, it will not have identity in a java class ref sense, it of course will still have identity in being a unique structure in memory at a certain address. This is just splitting hairs about Java nomenclature. |
| |
| ▲ | oddx 8 hours ago | parent | next [-] | | > it of course will still have identity in being a unique structure in memory No, it will not. The design allows multiple objects to share one structure in memory across multiple records, or not have such a structure at all (see Scalarization in the article). | | |
| ▲ | rf15 7 hours ago | parent [-] | | > The design allows multiple objects to share one structure in memory across multiple records, or not have such a structure at all Yes. I fear you are missing the point. |
| |
| ▲ | torginus 8 hours ago | parent | prev [-] | | I don't wanna badmouth Java people, but how they push the idea that this thing is some sort of genuine breakthrough that took multiple PhDs years of cutting-edge research to implement, when in fact they basically copied what .NET did from basically year 1, is not a good look. Again, not trying to turn this into a .NET vs Java thing, I'd have been much happier if they reached some new and interesting conclusions. | | |
| ▲ | gf000 8 hours ago | parent | next [-] | | > genuine breakthrough Well, it is - because they had to make it with almost perfect backwards compatibility for one of the most popular languages with trillions of lines of code produced over decades. Sure, adding it to a new language is not hard. Adding it to Java which has primitives, generics and boxing, finding ways that seamlessly cover the differences between objects and primitives, while trying to plan for the future is hard. As a general note, if you come to the conclusion that one of the best designer teams on Earth "basically copied what .NET did from year 1 is not a good look", then maybe your mental model needs adjusting on how these stuff works? Java has a public mailing list, you can browse through the related discussions. Implementation is the least of these things. But I can assure you they most definitely know what they are doing. | | |
| ▲ | tsimionescu 2 hours ago | parent | next [-] | | Except of course they are breaking backwards compatibility, in relatively subtle ways, for anything that uses the standard library wrapper types. So every use of Integer, Boolean , etc is an opportunity for either a compilation error or a runtime bug. | |
| ▲ | sysguest 8 hours ago | parent | prev [-] | | idk maybe java should adopt something similar to rust's "edition"? | | |
| ▲ | gf000 8 hours ago | parent [-] | | Correct me if I'm wrong, but Rust editions are a source code-level feature. So given you have the source code of newer and older rust code, you can compile them together. That's materially distinct from Java's model of basically dynamic loading already compiled class files. Though class files do have "editions", and there are extra code to deal with different versions. But still, it should be possible to e.g. send a new value class to an old library's class that has never heard of them, and that should just work. | | |
| ▲ | simonask 4 hours ago | parent [-] | | The important thing is that Rust editions affect semantics and name resolution. In such an analogy, JVM bytecode is the equivalent of Rust code - various semantics are baked in, but stuff like name resolution isn't (at least not completely). |
|
|
| |
| ▲ | coldtea 7 hours ago | parent | prev | next [-] | | >I don't wanna badmouth Java people, but how they push the idea that this thing is some sort of genuine breakthrough that took multiple PhDs years of cutting-edge research to implement, when in fact they basically copied what .NET did from basically year 1, is not a good look. Oversimplifying a big semantic and backend change to a huge codebase on which some of the most crucial customer and government and business systems depend on, and which has to be made as seamless, correct, and performant as possible, to "they just copied .NET", just because .NET has the same functionality, is an even worse look. It's a "HN "Dropbox is just rsync + some scripts"-style bad look. | |
| ▲ | misja111 8 hours ago | parent | prev | next [-] | | This is exactly what made it so difficult. It is much easier to have a feature like this from year 1 than to add it to a language that has grown and evolved for 18 years already. | | |
| ▲ | rf15 8 hours ago | parent [-] | | I agree with this sentiment. The work they put in deserves a lot of respect, and took a lot of effort, no doubt. It's just the framing they push to the public that could use some work. |
| |
| ▲ | DarkNova6 5 hours ago | parent | prev [-] | | The article is shit, the actual concept and roadmap goes well beyond the capabilities of C# or the CLR. |
|
|