Hacker News new | ask | show | jobs
by Grazester 3504 days ago
What exactly would be the benefit of this?
4 comments

No more problems with Oracle, C# features that Java is missing, such as type inference, proper generics, built in concurrency, operator overloading, first class optionals, ...
No more problems with Oracle, but what about Microsoft? Today we have the "cool" CEO, but that may change in the future. Remember Android started when Java was in Sun Microsystems' hands.

Apart from that, C# and Java are not very different from each other. You can get many of that "features" from other Java languages, no need to move away from the JVM, so you can still benefit from the largest community.

Their move to Open JDK should do away with the oracle issue no?
They are just cherry picking features, check the AOSP.
Better support for mixed managed/native code for one. There's P/Invoke, and .NET's byte code includes instructions for low level pointer manipulation (which C# exposes).

You could even compile C/C++ to CIL and obviate the need for some architecture specific binaries.

Actually, with the JNA (not JNI), one can invoke native code from Java even easier than in C# – you just define a Java Interface containing the relevant C functions, bind the library, and get an Object implementing it.
But that C/C++ code you're calling still needs to be compiled to native code. The GP was referring to .NET CIL (bytecode) supporting (by design) a large subset of C/C++ semantics.

The GP wants to avoid shipping N different binaries for N platforms in cases where the C/C++ code being called isn't already on the platforms.

I haven't used Managed C++ and I'm fuzzy on the details, but my understanding is that a fairly large subset of C/C++ can be efficiently compiled to .NET CIL. (Of course, the class loader for untrusted security contexts will refuse to load classes that use the type-unsafe/memory-unsafe portions of .NET CIL, just like the Java classloader for untrusted applets won't load classes making arbitrary JNI calls.)

Based on what I know about CIL and C++, the entirety of C and C++ can be compiled to [unverifiable] CIL. The sole exception is setjmp/longjmp - this might be doable on top of CLI exceptions, but I'm not sure.

And, indeed, VC++ lets you do just that. There are some bits of the standard library, mostly new stuff like threads and atomic, that had some issues, as I recall. But it's more about the amount of effort that's needed to target what's essentially a completely different platform.

The thing to know about CIL is that it has:

- structs;

- unions;

- raw data pointers, with pointer arithmetic;

- raw function pointers;

- dynamic stack allocation (like alloca in C);

- tail calls;

- exceptions with exception filters (arbitrary expression evaluation when deciding whether to transfer to a given catch-block or not) and finally blocks.

I'm actually curious if there's any language that cannot be compiled down to this efficiently.

Dynamic languages cannot be easily mapped to CIL, which is why IronPython was the genesis of DLR extensions, later added as standard part of .NET.
Dynamic languages typically aren't compiled, though. So either a language would have some kind of VM of its own (which is typically implemented in C, and hence can be implemented in CIL), or else they have some form of JIT compilation, which can use CIL as a target.
With C#, you can implement an IUnknown-derived interface completely in C++ (with `class` etc), and then define it in C#, and it transparently does all the mapping of members for you.

Yes, it's COM. People assume that makes it non-cross-platform, but there's nothing Windows-specific about COM, it's just a slightly higher-level ABI than C. So Mono supports it on Linux in the same manner. Not sure what the story is in .NET Core.

It would be worth it just to have LINQ
LINQ is cool and all, but it takes a lot of code to make that cool, so I would love to see how it affects battery life.
LINQ and Lambda expressions had me liking C# a lot back when I used it for an Entity Framework project I was on(well java got Lambda expression not too long ago though)
Why? The Java Stream API is superior to LINQ in every way, and a lot more extensible.

EDIT: Instead of downvoting, please point out how LINQ is actually superior?

the Stream API is nice, but it is not extensible. I guess you have mistaken LINQ with the SQL style LINQ queryes, but it is actually a pack of feautures, the basis of all is the Extension Methods. Every LINQ query is just syntactic sugar for composed of some extension methods to IEnumerable<T>. Without that LINQ is a bunch of extension methods giving an extensible fluent API much like the Streams API, but with true extensibility.

How do you extend the the java Stream API? for LINQ you just drop in a few extra extension methods for IEnumerable<T>, IOrederedEmumerable<T>, IQueryable<T>, etc.

How do you reverse a stream in java? (I know you need to evaluate the whole stream for that) You can write a utility method, but that will break the fluent readable logic. In C# you can do the very same thing, with the same method usable as an extension method keeping the readable fluent expression.

Starting a stream is a PITA itself, as it differs for Iterables, Arrays, Collections... Been there, done that. Not that with some utilities these could not be overcome, but it was an inferior experience to LINQ.

> How do you reverse a stream in java?

By calling .reverse() on it, and making it iterate the other way around?

> extension methods

Extension methods are something that’s really really problematic, and easy to create confusion with.

That said, I’d just import the functions I want to call on the stream into my local namespace with import static.

When i last used the stream api i don't remember it had a reverse functionality. Still that was an example only.

Edit: Just checked: https://docs.oracle.com/javase/8/docs/api/java/util/stream/S... no reverse().

Fyi for 1 year I have been working on a Stream API heavy app. Now working on a C# project with quite some LINQ. I know both sides to some extent...

How is stream superior? I cringe every time I move between Integer and int.
You made the bold statement, why don't you say something to support it.
Using a language not in the hands of Oracle.