News for hackers. Stuff that matters.
Re:Java 21 makes me like Java again by neonsunset on Saturday September 16, 2023 @09:46PM (#37540672)
Java is an inferior language to C#. To get a superior language you would likely have to go for Kotlin. There is no comparison, because Java gets features today that C# had for years. Not even mentioning having to retrofit green threads because adopting async/await (used by TS, Rust, Swift and other languages) is impossible at this point.
Re:Java 21 makes me like Java again by ecshafer on Saturday September 16, 2023 @10:21PM (#37540853)
F# is far superior to C# as a language. If you are going to jump to the .NET runtime that's the tool to go for.
Re:Java 21 makes me like Java again by ZeroClickOk on Saturday September 16, 2023 @10:51PM (#37541029)
apples and oranges
Re:Java 21 makes me like Java again by throy930493 on Saturday September 16, 2023 @09:51PM (#37540698)
C# is a better language, but the JVM is a significantly better ecosystem. Java will always be behind (and type erasure for generics is annoying enough as someone who started in C# that I don't think it will ever catch up) but between hotspot and being the preferred language of 2/3rds of the clouds I think it clearly wins.
Re:Java 21 makes me like Java again by anoy8888 on Saturday September 16, 2023 @10:41PM (#37540968)
If you like jvm, use closure instead. Java should be dead
Re:Java 21 makes me like Java again by hmottestad on Sunday September 17, 2023 @01:27AM (#37541809)
Sorry you’re down voted. Closure might not be what the author is looking for.

There are a lot of languages you can use with the JVM, Closure is one of them, Groovy, Scala and Kotlin are some more. Kotlin is gaining a lot of traction, especially since it’s backed by JetBrains.

Re:Java 21 makes me like Java again by sgt on Sunday September 17, 2023 @09:23AM (#37544814)
Clojure*
Re:Java 21 makes me like Java again by bcrosby95 on Saturday September 16, 2023 @11:10PM (#37541148)
Type erasure is why there's such a rich ecosystem of 3rd party languages that run on the jvm.
Re:Java 21 makes me like Java again by bmm6o on Sunday September 17, 2023 @04:43PM (#37549110)
Meaning that it would be harder for those languages on the clr because of generics? Would typing all generics as Object not be essentially equivalent?
Re:Java 21 makes me like Java again by throwaway2037 on Monday September 18, 2023 @04:49AM (#37553783)

   type erasure for generics is annoying
I too have spent significant time programming in both C# and Java. This complaint about type erasure in Java: When does it affect your daily life? There are so many craft workarounds available now that it hardly comes up anymore.

Also, would your opinion of Java significantly change if type erasure was removed or never existed?

Re:Java 21 makes me like Java again by throy930493 on Wednesday September 20, 2023 @11:09PM (#37592798)
I have much more experience in C# then Java FWIW, so you're welcome to take my opinions with a grain of salt. I've had a couple of really annoying errors with Beam serializers that took me way longer to debug then I thought was worthwhile.

I think it is a worse choice, it's too ingrained in the language and ecosystem to change. That and a couple of other nits would likely make me pick Java over C# or Go if I was starting a new project going forward.

Re:Java 21 makes me like Java again by thangalin on Sunday September 17, 2023 @12:20AM (#37541493)
Java, prints 1:

    Set<File> s = new HashSet<File>();

    s.add( new File( "c:/temp" ) );
    s.add( new File( "c:/temp" ) );

    System.out.println( s.size() );
C#, prints 2:

    ISet<FileInfo> s = new HashSet<FileInfo>();

    s.Add( new FileInfo( "c:/temp" ) );
    s.Add( new FileInfo( "c:/temp" ) );

    Console.WriteLine( s.Count );
C#, test fails:

    string path = "/tmp/filename.txt";
    FileInfo f = new FileInfo( path );

    File.CreateText( path ).Dispose();
    Assert.True( f.Exists );

    f.Delete();
    Assert.False( f.Exists );
https://en.wikipedia.org/wiki/Principle_of_least_astonishmen...

Languages have their strengths and weaknesses.

Re:Java 21 makes me like Java again by metaltyphoon on Sunday September 17, 2023 @01:14AM (#37541746)
FilInfo doesn’t override Equal operator in C#. HashSet will use it for comparison and those are two different object references so idk what the confusion is here.
Re:Java 21 makes me like Java again by thangalin on Sunday September 17, 2023 @01:50PM (#37547640)
A set is a collection of objects in which order has no significance, and multiplicity is generally also ignored. Observing the code alone, it isn't obvious that two FileInfo instances having the same path are themselves not equal. As has been mentioned elsewhere, FileInfo should not be hashable to prevent declaring it as a Set, avoiding the situation entirely.

C# isn't flawless. Java isn't flawless.

Re:Java 21 makes me like Java again by bad_user on Sunday September 17, 2023 @01:12AM (#37541734)
C# does not have virtual threads. What C# has is async/await. Which was nice for its time, but at the same time it's an error prone design, as computations should never start async by accident, blocking should be the default. C# also has no useful interruption model. Java's interruption model is error prone, but at least you can work with it.

Async/await also splits the standard library and the ecosystem in 2 (blocking vs non-blocking, or blue vs red), and it can't automatically update the behavior of old code.

The introduction of virtual threads in Java also works well with "structured concurrency", as seen with Kotlin's Coroutines. Kotlin's approach to concurrency is also superior to that of C#, actually. But what's interesting about Java is that its evolution is often one that involves runtime changes, lifting all boats. Java engineers preferred pushing more changes in the runtime, and somewhat ironically, the JVM ended up being the true multi-language runtime.

Java is a good case study of how languages should evolve. It has extreme backwards compatibility, and features being pushed are assessed for how they impact the whole ecosystem, including libraries or languages not in Oracle's control. Project Loom was developed in the open, compared to what Microsoft usually does.

Re:Java 21 makes me like Java again by neonsunset on Sunday September 17, 2023 @01:21AM (#37541777)
Do give async/await in C# a try, it has all the structured concurrency features other languages have to invent APIs and special syntax for :)

(if you want to take a look at good structured concurrency, you might be interested in Swift implementation)

Re:Java 21 makes me like Java again by bad_user on Sunday September 17, 2023 @01:42AM (#37541875)
I worked with C# professionally.

The async/await syntax works with the language's other statements, but for a long time it had gotchas. It doesn't qualify as "structured concurrency", and it has the aforementioned issues — it's (accidentally) error-prone, it splits the ecosystem in two, and has no interruption model.

I am not familiar with Swift, but I think you can hardly beat Kotlin's implementation. This is a good introduction from Kotlin's lead:

https://www.youtube.com/watch?v=Mj5P47F6nJg

Re:Java 21 makes me like Java again by neonsunset on Sunday September 17, 2023 @01:49AM (#37541900)
What do you mean by "it splits the ecosystem in two"? I never observed such split, certain methods intentionally offer sync and async variants.

Interruption is achieved through cancellation tokens and has to be handled by consuming methods. There is no way around it because interrupting execution at an arbitrary point would lead to all kinds of issues regardless of the language (unless it implements some form of transaction abstraction and rollbacks all uncommitted changes).

Re:Java 21 makes me like Java again by pjmlp on Sunday September 17, 2023 @03:45AM (#37542520)
Except when there is only one version and someone has to write the transition code.
Re:Java 21 makes me like Java again by Too on Sunday September 17, 2023 @03:58AM (#37542611)
Re:Java 21 makes me like Java again by kamma4434 on Sunday September 17, 2023 @04:44AM (#37542884)
> regardless of the language

That’s a choice, but it is unfortunate for the programmer. The truly right way to do it all is like Erlang does - where all processes are cancellable and nothing bad happens.

Re:Java 21 makes me like Java again by bad_user on Sunday September 17, 2023 @05:22AM (#37543105)
Those "cancellation tokens" from C# are a band-aid.

In Java, you don't need to initialize those "tokens" manually, because the interruption signal is baked into Threads. Moreover, a lot of the standard library cooperates with Java's interruption, which is why you see plenty of methods throwing `InterruptedException`; and it's also reflected in types such as `CancellableFuture` or the `Flow.Subscription` (reactive streams). Of course, user-level code that isn't well-behaved, can end up catching InterruptedException, or resetting the interruption signal, without actually interrupting. This makes Java's interruption model somewhat error-prone, but it's workable, and at least it's baked in.

Note that interruptions could also be preemptive, as you don't necessarily need cooperation. If you think of the call-stack, or flatMap/SelectMany in reactive APIs, the compiled code could check the interruption flag automatically and interrupt the call chain.

And resource leaks aren't necessarily a problem, if the interruption protocol is well-thought-out. In Java, try/finally still works in the presence of interruption, since at worst you get an `InterruptedException`. It's not ideal because you can interrupt the interruption process, and in truth the ideal would be for interruption to be its own communication channel, complementing that of exceptions. But it's totally doable, and here I am familiar with several libraries from Scala's ecosystem, namely Cats-Effect, Monix, and ZIO that show it (with limitations imposed by the runtime).

Either way, what C# provides is basically next to nothing. In fairness, some C# libraries tried fixing it, such as Rx.NET, but it's not enough. And the aggregate result in the .NET ecosystem is that interruption is not something people design for. Like what to interrupt a network socket? This ends up being a setting, presented as a timeout in case of inactivity, as a configuration of the connection, instead of a higher-level generic function that can be applied on the consumer side. And the probability for resource leaks goes up actually, because in the presence of concurrent races, you really need interruption.

Re:Java 21 makes me like Java again by pjmlp on Sunday September 17, 2023 @03:43AM (#37542513)
And don't forget to bookmark David Fowler guidelines to avoid all the gotchas that everyone falls into while using async/await.
Re:Java 21 makes me like Java again by throwaway2037 on Sunday September 17, 2023 @07:53PM (#37550601)
Re:Java 21 makes me like Java again by orra on Sunday September 17, 2023 @05:36AM (#37543178)
But async/await is special syntax. I agree it's a nice improvement over using Task APIs directly.
Re:Java 21 makes me like Java again by pregnenolone on Sunday September 17, 2023 @08:49AM (#37544498)
There shouldn't be a reason for a managed language to introduce function coloring. It's horrible.
Re:Java 21 makes me like Java again by kaba0 on Sunday September 17, 2023 @02:24AM (#37542084)
You can’t decide language superiority by the number of features. While C# indeed has many cool features, I do think it has already went into C++ territory where all the different, independently cool features have very non-trivial interactions that make it very hard to reason about.

Also, async-await is not a good thing — virtual threads are superior in every way in case of a managed language.

Re:Java 21 makes me like Java again by neonsunset on Sunday September 17, 2023 @03:27AM (#37542420)
Why async/await is not a good thing? What makes green threads better? Do you know the difference, the issues async/await addresses that green threads simply do not?
Re:Java 21 makes me like Java again by pjmlp on Sunday September 17, 2023 @03:45AM (#37542524)
Read David Fowler's guidelines.
Re:Java 21 makes me like Java again by winter_blue on Sunday September 17, 2023 @06:00AM (#37543319)
Do you have link to any specific article of his (on virtual threads vs async)?
Re:Java 21 makes me like Java again by pjmlp on Sunday September 17, 2023 @07:15AM (#37543785)
Re:Java 21 makes me like Java again by kamma4434 on Sunday September 17, 2023 @04:47AM (#37542900)
Because they were created to manually run multiple threads on one physical thread, which is the kind of decoupling that a VM/OS is supposed to do for you.
Re:Java 21 makes me like Java again by tomp on Sunday September 17, 2023 @09:22AM (#37544805)
This is code with async/await:

  async fn read_file(filename):
    f = await os.open(filename)
    let data = f.read()
    await f.close()
    return data
this is equivalent code with green threads:

  fn read_file(filename):
    f = os.open(filename)
    let data = f.read()
    f.close()
    return data
As you can see, async/await is pure syntactic noise, it doesn't convey any important meaning.
Re:Java 21 makes me like Java again by neonsunset on Sunday September 17, 2023 @09:25AM (#37544831)
Async enables clear contract for a type that represents a delayed result.

Better implementations offer eager execution and allow to easily interleave multiple concurrent futures/tasks, like C#. Green threads on the other hand are a workaround to deal with blocking for the most trivial case of cooperative multi-tasking, offering little beyond that.

Re:Java 21 makes me like Java again by kaba0 on Sunday September 17, 2023 @09:38AM (#37544953)
> Async enables clear contract for a type that represents a delayed result.

It's not really a type, otherwise it would be something like Future<T> in Java and plenty other languages. It is usually implemented as a transformation to a state machine.

Also, Loom is M:N and calling them green threads doesn't give you the whole picture at all. Not exactly sure what you mean by easily interleave -- functionality wise the two is more or less equivalent. You just get to keep your simpler mental model (and tooling) with virtual threads.

Re:Java 21 makes me like Java again by tomp on Sunday September 17, 2023 @10:51AM (#37545614)
with green threads, every result is "delayed" and available exactly when you want it (i.e. as indicated by naive reading of the control flow in code)

> eager execution, interleave multiple concurrent tasks

green threads give you that for free

> deal with blocking

blocking is an implementation detail; both async/await and green threads can be implemented on top of either traditional blocking IO, or callback/non-blocking IO

I'd be even happier to discuss concrete code blocks / examples, that's where the superiority of green threads truly shines

Re:Java 21 makes me like Java again by ncruces on Sunday September 17, 2023 @11:24AM (#37546021)
Re:Java 21 makes me like Java again by pjmlp on Sunday September 17, 2023 @03:41AM (#37542497)
C# is turning into C++, sadly.

It appears every six months there must be new language features being added.

Re:Java 21 makes me like Java again by kamma4434 on Sunday September 17, 2023 @04:38AM (#37542848)
Async/await is an inferior product compared to threads, for so many reasons (function coloring and meaningful stack traces come to my mind). Keeping the same interface as native thread, plus structured concurrency, are best of breed.

Many uncancellable threads + mutability + goroutine panic kill it all are serious issues for golang.

Re:Java 21 makes me like Java again by imtringued on Sunday September 17, 2023 @05:33AM (#37543157)
Java 21 doesn't retrofit green threads though. Quasar [0] is a library that implemented fibers for Java and the main developer pron has joined the OpenJDK development team. All that was necessary for first party support is to make the JDK libraries yield when blocking.

Adopting async isn't impossible at all, there is very little demand for it.

[0] https://docs.paralleluniverse.co/quasar/