5 ms·
> Type inference: because having to write out every type, however obvious, is an incredible waste of time. Person me = new Person(); is ridiculous. let me = new
by aazaa 6y ago
> Type inference: because having to write out every type, however obvious, is an incredible waste of time. Person me = new Person(); is ridiculous. let me = new Person(); may seem like a small improvement, but spread over the body of an entire program and generalized to all sorts of contexts means that type annotations become a tool you employ because they’re useful — for communicating to others, or for constraining the program in particular ways — rather than merely because the compiler yells at you about something it should know perfectly well.
Type inference was a major revelation to me as well in Rust. I was reluctant to learn the language because of my experience in Java with its high ceremony everywhere, mostly due to lack of type inference.
The first thing I noticed with Rust was type inference. It gives the entire language a distinctly high-level, almost scripting language feel - modulo ownership.
- aembleton 6y agoYou might enjoy Kotlin. Type inference, with full compatibility with Java libraries and ecosystem. Similar to Rust, you can just write `val me = Person()`. No need for the new keyword.
- eru 6y agoThough keep in mind that not all type inference is created equal. Ie what you find in Haskell or OCaml is much more powerful than what eg Go gives you. (In Go type inference only works 'forward'.) (I don't know enough about Kotlin to know what kind of type inference it has.)
- edem 6y agoI love Kotlin. I also love Typescript. I'm yet to try Rust, but I'm positive that I'll love it as well. They are all born to solve the same problem I think, only from a different perspective. I use Kotlin every day and the more I learn the happier I am. I'd never go back to Java if I can help it.
- jenscow 6y agoPersonally, I prefer the type names to be at the start of the line, so I don't need to scan to the end of the line (or guess based on a function name). But I agree, it's a waste to type it all out. So I use an intelligent IDE that reduces the repetitive typing: So typing `Person.var` gives me `Person person = new Person();` or typing `Person person = ` will suggest `new Person()` Sure, it doesn't look as appealing when creating an new object, however I get a better information when you've written something like: Person me = something.GetOwner(); rather than: let me = something.GetOwner();
- II2II 6y agoIn the case of Java you have the option of using "Person me" or "let me". Simply stick to the convention of explicitly stating the type at least once when declaring a variable. I have also noticed that Python allows developers to note the type (at least when defining functions). It's not necessary and I don't think the language uses it, but at least it offers a standard mechanism to note it in the code.
- mannykannot 6y agoI think you are making a good case for a somewhat different point: we have long reached the point where the tools we use for writing and reading programs can give us all sorts of semantic information on demand, and language syntax design should take advantage of the fact that everything the programmer needs to know need not be forced into just one flat page-of-text view of the code. Alan Kay made essentially this suggestion when the requirements for the language that became Ada were being debated, but it was not taken up at that time.
- jenscow 6y agoBut that doesn't mean hide everything from view just because it can be accessed via a keyboard short-cut or mouse movement - my eyes are faster and easier to move. Also, code isn't read exclusively in an IDE, nor the same IDE that I use.
- vlovich123 6y ago
- svieira 6y agoOnce you get to Java 11+ you get `var`, so you can spell it `var me = new Person()`.
- mgkimsal 6y agoWas recently on a project that just migrated to java 11 (mid august of this year) from java 8. Someone has started to try to introduce 'var' and is getting push back from others. "it doesn't match current style" and "could be confusing" were some 'concerns'. When you're trying to do InternalFormatParserCustomForClientABCDEF customerFormatParser = new InternalFormatParserCustomForClientABCDEF(param1, param2); you really start to hit readability limits, and formatting things like going over nominal line lengths and such. var customerFormatParser = new InternalFormatParserCustomForClientABCDEF(param1, param2); is certainly easier on the eyes, and the compiler can still known and infer what type 'customerFormatParser' is.
- nkohari 6y agoYeah, the same debates happened in C# back when the 'var' keyword was introduced. People just fear change, but once they got hands-on experience they realized the value.
- metters 6y agoJust don't use it together with lombok.