4 ms·
Java lacks default arguments for functions, which makes that particular API infeasible without adding support for such. They could generate "withFoo" methods or
by tadfisher 7y ago
Java lacks default arguments for functions, which makes that particular API infeasible without adding support for such. They could generate "withFoo" methods or even builder types, but that would butt up against developer preference, so it's probably best to leave it up to developers or a third-party library.
Kotlin has a very similar feature in its data classes, FWIW.
- skissane 7y ago> Java lacks default arguments for functions, which makes that particular API infeasible without adding support for such I really wish Java would add both default arguments for functions, and also support for passing function arguments by name. One of these days...
- nine_k 7y agoThis goes against the very idea of method signatures in Java an overloads based on them. An attempt to add it will likely bring in way, way more backwards-compatibility pain than any benefits are worth. Writing simple methods with few arguments helps.
- diegof79 7y agoYou can have named parameters and backward compatibility with method signatures, Groovy does that [1]. Ok, it cheats a little bit because named params are actually a syntax sugar for Maps. But other languages do the same (e.g. Python) and is very useful too. And if you can type check the map (as in Typescript) the functionality is equivalent to named params. [1] http://docs.groovy-lang.org/latest/html/documentation/#_named_parameters http://docs.groovy-lang.org/latest/html/documentation/#_name...
- viraptor 7y agoPython has real keyword parameters, not just a syntax sugar. They're just presented as a dict when you want to see them all. And you can splat that dict when calling. Ruby before 2.7 has a syntax sugar where keyword parameters are the same as a trailing hash. But that's changing as well.
- thu2111 7y agoThe JVM has all the machinery required to do this right, though: invokedynamic. Kotlin provides named arguments and a copy method on data classes/records that uses them. You do indeed have binary compatibility issues with them, but it's not a fundamental problem. It's just that it sits at the intersection of: - Problems that only affect library writers, rarely a high priority for language designers (Java being a exception) - Obscure JVM knowledge and new techniques - Poor Android JVM holding back the ecosystem by discouraging any bytecode techniques that post-date Java circa 2010.
- lmm 7y agoDoes Kotlin have anything to let you treat data classes in a generic way like you can do with Shapeless in Scala? That's where the real power comes IME - e.g. look at Circe where you can derive JSON encoders/decoders for arbitrarily nested case classes at compile time, so you have zero boilerplate but still get compile-time type safety that stops you from accidentally trying to serialise a file handle or semaphore.
- nitrogen 7y agoThere is the currently experimental kotlinx.serialization library that generates e.g. JSON codecs at compile time. Not sure if that fits the bill.
- lmm 7y agoLooks like it's doing annotation processing at compile time? The trouble with that is that you end up with "magic" code like calling methods that don't appear to exist, and in order to be able to work on the serialisation itself (e.g. to add a new format) you have to understand the "magic", because there isn't a good intermediate abstraction. The great advantage of Shapeless is that all the "magic" is in the library itself and it spits out a generic representation of your data classes in record form; you can implement a new serialisation format in 100% vanilla Scala without having to worry about any macros or annotations or anything.
- thu2111 7y agoIt's not annotation processing, it's a compiler plugin with IDE support. So pretty deeply integrated. It avoids some of those issues. The kotlinx.serialisation design is pretty nice. It's basically what you describe. The compiler generates generic code that can call into a variety of "codecs" but they don't have to actually be serialisation specific. So there are codecs for JSON, protobufs, CBOR etc but you can also do arbitrary object graph transformations with the framework. A deep clone being a simple hello-world type example. I've never used Shapeless but it sounds pretty similar in some ways. https://github.com/Kotlin/kotlinx.serialization https://github.com/Kotlin/kotlinx.serialization