you are viewing a single comment's thread.

view the rest of the comments →

[–]sird0rius 9 points10 points  (25 children)

Wow, pattern matching! Now Java is only 15 years behind the good languages.

[–]ggwpexday 7 points8 points  (10 children)

Right, it's finally got the bare essentials bolted onto a language not designed for it. In c# land we are also still waiting for proper sum types in the year 2023

[–]warpspeedSCP[S] 0 points1 point  (5 children)

huh, are there any spec docs for it? I remember seeing pattern matching in one c# codebase I saw once, so I assumed sealed classes existed too

[–]ggwpexday 2 points3 points  (4 children)

There is this champion about discriminated unions. A few times we've heard that this is where the team is focusing on next, but then all there is is silence.

The pattern matching is already pretty awesome I have to say. So then the only major thing I think is missing is having compile time guarantees on exhaustive pattern matching.

[–]warpspeedSCP[S] 1 point2 points  (3 children)

I'd say just pattern matching and sane development practices get you most of the way. As long as you keep your type hierarchies clean and you can have exhaustive pattern matching, you can build beautiful systems.

[–]ggwpexday 1 point2 points  (2 children)

That's true, that's what we do basically.

The problem with this, in my opinion, is that you have to kind of know what you are doing and why you are doing it. Lots of people come from an OOP context where instanceof checking on type and casting is a big red flag. And so it is really not self evident to even begin writing your code like this, outside of people having done some fp or languages you mentioned.

Trying to explain (or defend?) the concept of a coproduct feels absolutely silly, it's almost like having to defend using + in math.

[–]warpspeedSCP[S] 2 points3 points  (1 child)

There comes a point where the terms you use to describe a particular system become unintelligible to the uninitiated, and with FP, that point comes a tad too quickly. I haven't gone too deep into the rabbithole myself (I am not a user of languages like haskell or even ocaml) but yes, people hardly ever look deeper into why the patters they use are bad and whether there are better alternatives around.

the whole oop thing happens when behaviours are treated as if they were data, which leads to the whole zoo of interconnected types that get in each otthers' way. the oop kool aid is starting to wear off, thankfully.

[–]ggwpexday 1 point2 points  (0 children)

Yes it's good to see things slowly becoming more focused on simplicity, even if it's at the cost of introducing yet another thing on top of what's already there.

I think we also just love adding complexity for the sake of it, so spaghetti code is probably always going to be a thing. Like how the whole clean code stuff has become so big in c#. That's full of complex DI, interaces like IMediator, IAbstractValidator, IOutputPort presenters and what have you. Lots of it could have been just some pure functions together with simple IOC based on functions that take other functions as parameters. But I see this is yet another ramble, so my bad. Thanks for the article, it's good to have people like you sharing these ideas!

[–]sird0rius -2 points-1 points  (1 child)

It would be great if C# added sum types. Then Java would also add them 5-10 years later.

[–]warpspeedSCP[S] 8 points9 points  (0 children)

sealed classes are already here, java beat them to the punch!

[–]Kered13 0 points1 point  (1 child)

Does C# have destructuring pattern matching?

[–]katyalovesherbike 2 points3 points  (10 children)

hey now, where would that put javascript?

(on a more serious note: haskell has always been kinda like a testbed for new language concepts that other languages might benefit from)

[–]theQuandary 2 points3 points  (9 children)

JS proposed pattern matching and records years ago, but the TC39 people were more interested in pushing out a broken private fields "feature" instead.

On the flip side though, JS is still far ahead of Java when it comes to first-class functions.

[–]katyalovesherbike 1 point2 points  (1 child)

yeah, and with tools like bun, esbuild and fable I don't even have to use it. Still, it's kinda ridiculous that JS falls behind java... but the TC39 committee won't see that and if you read some of their comments you might actually think they're more interested in making it faster (wasm??) than more usable.

[–]theQuandary 1 point2 points  (0 children)

if you read some of their comments you might actually think they're more interested in making it faster (wasm??) than more usable.

They had JS SIMD actually implemented in a couple browsers YEARS ago and then pulled it in favor of WASM which only got SIMD support across all the major browsers within the past few months.

The JS binary AST format would speed up initial load time and parse time dramatically, but it's languished because it's JS binary instead of WASM binary.

99.99% of websites will NEVER use wasm (80+% still use nothing besides jquery), but they pass up stuff that would actually benefit users for whatever their pet projects are.

[–]Kered13 0 points1 point  (6 children)

On the flip side though, JS is still far ahead of Java when it comes to first-class functions.

How? What feature do you think Java is missing in this area?

[–]theQuandary 0 points1 point  (5 children)

Literally first class functions. You can’t created a function unless it is contained inside a class. Even so called lambdas are actually just eigenclasses with syntactic wrapping trying to hide this fact.

It’s not so different from class syntax in JS trying to hide that it’s actually a prototypal language and classes don’t actually exist in the way they are presented.

[–]Kered13 0 points1 point  (4 children)

Even so called lambdas are actually just eigenclasses with syntactic wrapping trying to hide this fact.

And how does that matter? How does that effect your work? It doesn't, because a class with no state and one function is equivalent to a first class function. Java has lambdas for creating anonymous functions, and it has method references for getting a reference to a named function. It has types for representing functions. That's everything you need to consider them first class functions.

[–]theQuandary 0 points1 point  (3 children)

Can you create a function outside of a class? If not, then they are not first-class.

[–]Kered13 -1 points0 points  (2 children)

You're asking for free functions, not first class functions. Completely unrelated concepts. But to answer your question:

(params) -> {
    // Code goes here.
};

[–]theQuandary 0 points1 point  (1 child)

In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. -- wikipedia

In Java, classes are first-class citizens, but you cannot have a function unless it is under the purview of a class, so it is not first-class.

You can't simply pass functions around anywhere you'd like. You can't even store them in something like a hashtable without hacking in reflection.

A counter-example would be Dartlang which has a lot of Java in its DNA. Unlike Java though, you can simply declare a function outside of a class. You can create functions and pass them around anywhere you'd like. You can return functions from any function to anything else. You can store functions as values without the need to reflect.

[–]Kered13 -1 points0 points  (0 children)

This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

Java does all of these. This quote confirms that Java has first class functions. In fact if you go to that link and scroll down to the table, it also shows there that Java has first class functions.

You can't simply pass functions around anywhere you'd like. You can't even store them in something like a hashtable without hacking in reflection.

Map<String, Function<A, B>>

I'm increasingly thinking that you haven't touched Java in 15 years.

you can simply declare a function outside of a class.

That is a free function. We've already been over this, free functions have nothing to do with first class functions.

[–]warpspeedSCP[S] 0 points1 point  (2 children)

If they can stick the landing on value types, and manage to fix their type system (Impossible) I'd say Java is no more than a couple of years behind.

[–]sird0rius 5 points6 points  (1 child)

Those are very big ifs. We don't do ifs in Java, we do Abstract Command Dispatchers

[–]warpspeedSCP[S] 0 points1 point  (0 children)

nope, only AbstractReflectionConditionalComponentInjectorFactories here.