Ask HN: Why don't languages such as Java support json-like object literals?
Why don't GC languages such as Java support json-like object literals? (For reference check out the code snippet on this stack overflow question: https://stackoverflow.com/questions/34727831/create-a-json-literal-in-java-without-need-for-existing-class)
My conclusion is that the processing would be solely at the parsing stage and not at the runtime stage of the program, so there shouldn't be any conspicuous perf hit in including this feature.
Note that this is all assuming that the programmer would in any case use the type Object to be agnostic of types specified within their json-like object even if they didn't have access to an object literal.
Are there some unforeseen issues (perf and non-perf related) to accommodating such object literals?
FYI this question is not just about Java, but extends to other GC languages (such as Golang, etc).
I've had this a few times with other programming languages, why doesn't it have guard clauses like erlang, or macros, or X and Y?
I think the reason is that the people who maintain a language got already plenty of work and you try hard to avoid 'Feature creep', meanwhile still satisfying most of the people who uses it(or not...).
You could as well implement the feature yourself, see if people like it.
But just as you've mentioned Java, I have to tell you something. Java took like a decade to implement lambdas(or more). They need to reach consensus, agree a lot of things. Implementing things is easy, agreeing is hard. Most of programming languages got a lot of people with very different interests.
There is a performance hit on having this feature in the compiling phase. It's yet another feature they will need to support. The work to handle this is a huuuge language such as java is absolutely huge, again, not so coding wise, but also the whole ecosystem. People would even have to update their books...
Like if I had a programming language, I would never implement what you said. I don't find it a nice feature.
It seems a basic thing though. Like String and Number and Array literals (these are also Objects in JavaScript).
I think the answer is more likely to be age and stagnation.
[1] https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...
[2] https://docs.microsoft.com/en-us/dotnet/csharp/programming-g...
1. Take the type definition from the point of use, not the declaration. For example, if the object literal is being assigned to a variable, its type is the type of the variable. If it's being passed to a function, its type is (a subtype of) the type of the function parameter. If it's being returned, it's the return type of the current function. C/C++ and Kotlin do this, as does Scala (I think; I don't know the language well).
2. Use structural subtyping. In the type system, allow the possibility of expressing "HasField x && HasField y && HasField z". Then the type of the object literal is simply an anonymous type that includes those predicates. Ocaml and (I think) Go do this.
For a programming language like Java which is used in really serious places like banks, medical equipment, and all systems which last more than 30 years from now on, it can't remove the feature once added because it break the existing code.
So the it's reasonable for the Java to be conservative at adding a feature just because it's trending right now.
Java is a pretty rigid and verbose language in general.