3 ms·
After using Haskell, using Java makes me very sad... In Java's OO paradigm, instance variables are inherently defined separately from their initialization, mak
by Peaker 13y ago
After using Haskell, using Java makes me very sad...
In Java's OO paradigm, instance variables are inherently defined separately from their initialization, making it quite difficult to make sure they're always used when initialized, properly.
The nullability stuff bites me over and over :(
Forgetting to use .start() on a thread is a stupid runtime error, instead of using some design where it isn't possible to forget .start() or at least get a compile-time error.
I am also using a lot of C in parallel, and with the C preprocessor, I am finding that C is often more expressive and nicer to use than Java!
Everything takes ridiculous amounts of code.
For example, in Haskell, I can write:
threadIds <- replicateM 10 . forkIO . forever $ do
code .. here ..
To get a thread-pool of 10 threads looping around my code, and put their thread ids in a variable.
In Java, this requires so much effort!
Not to mention the utter lack of sum types and pattern-matching, making tons of Java code that looks like:
if(msg.what == Foo) {
msg.obj1 ...
msg.obj2 ...
} else if(msg.what == Bar) {
..
}
arrrg!
- mercurial 13y agoFor thread pools, use ExecutorService or the Guava flavour. The lack of pattern-matching is a pain, but note that you can use Enum values in switch statements.
- Peaker 13y agoAs is often the case, writing the code from scratch in Haskell is easier than reusing a library in Java.. Android uses named integers rather than enums, due to some issue with Java enums.. And the switch would only help a bit, the main problem is needing to correctly interpret the associated payload according to the message type. That's what sum types give you, which Java doesn't properly have. They could be emulated with visitor pattern, if one is masochistic...
- sixbrx 13y agoI never understood why Java didn't go to proper sum types when it introduced enums. They all have to have the same shape, just distinguished by their method implementations, same as always in OO. What kind mechanism for describing alternatives is that?