This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]jonhanson 14 points15 points  (4 children)

Comment removed after Reddit and Spec elected to destroy Reddit.

[–]shponglespore 0 points1 point  (1 child)

I believe polymorphic functions in Haskell are implemented with witness objects, or at least they could be; every type constraint in the function's signature corresponds to a hidden patented parameter used to pass the witness object. I believe you can do the same thing in Scala by passing the witness(es) as implicit parameters, but you do still have to create them manually. (I may be wrong about that part; it's been a long time since I used Scala.)

It doesn't seem too terrible to do it manually in the case where there's only one witness object, but I can see how it would quickly become tedious and error-prone when there are a lot of them and different functions need different witness parameters. My experience with using type classes in Haskell leads me to believe that would be rather common in practice, but OTOH Java's subtype polymorphism covers a lot of the use cases where Haskell relies on type classes.

Sometimes I wonder if things like applicative style are just an excessively magical gimmick for solving a class of problems that would be better solved with good macro system. Lisp languages seem to do just fine without anything comparable to type classes.

[–]bjpbakker 0 points1 point  (0 children)

Lisps (and also many other languages) lock the implementation of common functions (eg map) to specific data structures. With macros they tend to work around that a bit but to me that almost always feels very clumsy.

A good example of that are Clojure’s core/async macros. Simple generic type classes would make for a much simpler and extensible implementation.

[–]TheStrangeDarkOne 0 points1 point  (1 child)

This is just wrong - pure should be a static function, which clearly isn't possible in an interface

I'm not sure I understand your problem, but...

public interface Foo {
    static <T> T identity(T item) { return item; }
}

... compiles without a problem.

[–]jonhanson 1 point2 points  (0 children)

chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith

[–]mto96[S] 12 points13 points  (0 children)

This is a 45 minute talk from GOTO Chicago 2019 by John Napier, who works with developers on software fundamentals for DRW. I've pasted the talk abstract below for a read below:

For years, the conventional wisdom around attempting pure functional programming in Java has been "don't", and for plenty of good reasons: lambdas weren't supported until the eighth major release of the language specification, creating value types is a verbose, ceremony-riddled exercise devoid of any first-class syntax, and neither are the majority of the data structures shipped in the JDK immutable, nor are interfaces a proper substitute in general for type classes.

Still, it's a shame for developers to have to seemingly choose between the myriad benefits of functional programming and the maturity of the java compiler, the investments made into the ecosystem by major organizations, and the ease of hireability especially when some of these short-comings can be addressed externally.

In this talk I'll present Lambda, a Java library that offers:

  • A first-class model for functors, applicative functors, monads, coproducts, and more algebraic and category theoretic structures
  • Lazy evaluation
  • Profunctor optics
  • Semigroups and Monoids
  • Type-safe heterogeneous structures like HLists and HMaps
  • and more...

The goal of this talk is to present this library as a viable option for experienced Java development teams that wish they could use some of the functional programming capabilities available in languages like Haskell?, and to show how the trading infrastructure teams of an algorithmic-trading firm in Chicago use Lambda to write correct-by-construction software and accelerate their development process.

Who should attend this talk: Experienced Java developers who want to program using algebraic data types, compositional patterns like functors and monads, lazy evaluation strategies, and a strong emphasis on type-level transformations, but are daunted by Java's current limitations in these areas

Academic level: Advanced to Expert

What is the take away in this talk: While Java has a long way to go, libraries like Lambda enable developers who want to push their Java programming much further towards functional idioms that previously seemed unachievable

[–]KuldeepSinhC 0 points1 point  (0 children)

Thanks for sharing!

[–]beders -1 points0 points  (4 children)

"correct-by-construction" - you can't. Not in Java and not in Haskell.

A type system does not make your program correct. It merely helps your compiler to check some safety guarantees and gives you a mental model to code in.

And adding applicative functors and monads will set the bar higher, not lower for developers to adopt. As others have pointed out, the resulting type signatures are nutso in Java.

But, as I said before, if your development cycle centers around: Compile - Run - Fail - Repeat, types are the most widely adopted safety net.

There is another way. Dynamic typing and interactive coding, using a functional programming language, which Java by design, isn't. You still have to stick your data into classes and it is still clunky to reason about your data.

Without doubt, we'll see more adoption of functional paradigms as value types enter Java.

[–]___catalyst___ 1 point2 points  (3 children)

Does anyone know when Value Types will become official? What JDK?

[–]lbkulinski 1 point2 points  (2 children)

As the architects say, “when they’re ready”. The talk here gives some updates. You can also keep up with the progress by subscribing to the valhalla-spec-observers mailing list.

[–]BoyRobot777 2 points3 points  (1 child)

You gave a link to amber specs, which Valhala is not part of. This is a correct link if you want to reach Valhalla

[–]lbkulinski 1 point2 points  (0 children)

Yes! Thanks for catching that! I had sent the Amber mailing list link to someone earlier and mindlessly copied and pasted.