#What advantages are there to using FP types like Eithers, Monads, etc.

1 messages ยท Page 1 of 1 (latest)

warped jay
#

Is there any advantage to using these types?

hearty grailBOT
#

<@&987246841693360200> please have a look, thanks.

hearty grailBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

surreal jolt
#

If anyone just says type safety I will wring you out. Think harder

humble gull
#

These things are immutable and can improve code readability (with proper usage). Also, due to immutability, it is simpler to implement concurrent algorithms. Additionally, you can explicitly show more information about types with them (like Optional, which indicates that there can be no value) or perform proper error handling.

However, since Java has overhead on classes, it can impact the performance of an application, especially when dealing with a large number of allocations. Project Valhalla aims to significantly decrease this overhead with Value classes.

EDIT: spells... also merged in one message cat_thumbs_up

warped jay
surreal jolt
#

when you have an Either<A, B>, you "have to handle the error case"

#

but that is also true for checked exceptions

#

the distinction is that in some contexts, the Either is "composable"

#

that is, you can chain some Either returning operations in lambdas or otherwise checked-exception-unfriendly pieces of code

#

in the situations where Either + the monad abstraction orginated, they are the "only" error mode

#

so they give a stronger guarentee than in Java where you can't actually know from a type signature that an exception wont be thrown

#

Monads are useful when you want to write code that is generic, in some way, over different types of containers of things

#

so Future<T>, List<T>, etc

#

but in practice....

#

not exactly proven to be useful

#

i mean, certainly can reduce code duplication when you have that stronger type system

#

see - scala ZIO and similar

#

but Java does not have higher kinded types or implicits so its pretty hard to actually make use of the common properties of different containers

warped jay
#

Okay thanks ๐Ÿ‘

warped jay
gleaming apex
warped jay
#

How it normally is? Not using pure functions, immutable class instances, monads, pattern-matching etc.

#

Basically OOP instead of FP

surreal jolt
#

actual research is basically nonexistant on everything

#

my >opinion< is that in Java the pure Either is not useful, but representing an error condition through a return value is increasingly valid

#

so don't use Either, make your own Either

#
sealed interface UserRegistrationResult {
    record Success(User user) implements UserRegistrationResult {}
    record UserAlreadyExists(User user) implements UserRegistrationResult {}
    record FailedToTalkToDB(SQLException e) implements UserRegistrationResult {}
}

UserRegistrationResult registerUser(String email, String password) {
    ...
}
#

but it is also valid to throw the exception

#

this is the best blog post on the different error handling mechanisms

surreal jolt
#

FP exists without monads

#

and frankly OOP vs FP is a false dichotomy

hearty grailBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you ๐Ÿ‘