Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite: Use type inference when possible #287

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

guicamest
Copy link
Contributor

Description of changes:

Java 10 introduces var which uses type inference and saves the developer from specifying the type of a variable. This also helps to remove some imports which are only used to declare the type of a variable.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@guicamest guicamest force-pushed the useTypeInferenceWhenPossible branch 2 times, most recently from 2e0be57 to 6a97554 Compare November 9, 2023 16:09
Copy link
Contributor

@markjschreiber markjschreiber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor. Thanks

@markjschreiber markjschreiber merged commit 9329583 into awslabs:main Nov 9, 2023
1 check passed
@guicamest guicamest deleted the useTypeInferenceWhenPossible branch November 9, 2023 16:48
@stefanofornari
Copy link
Contributor

stefanofornari commented Jan 17, 2024

I would have loved to discuss this change. Use of var in Java is quite arguable and there are good reasons not to use it. It is actually pretty bad practice IMHO (and not only) which lowers the quality of the code with no real advantage.

@markjschreiber
Copy link
Contributor

I know there is active debate about the use of var in Java and I didn't originally like it. However, there are many strictly typed languages that allow compile time type inference including Scala, Kotlin and even Rust. I'm willing to defer to the designers of these languages (and Java) that the pros probably out weigh the cons.

Dynamic inference at runtime is a terrible idea though, I hope Java never goes there!

@stefanofornari
Copy link
Contributor

I agree to let the debate about introducing var in Java to who designs the language. I even appreciated the attempt to modernize the language. But this is not the point for the project. The point is whether this PR brings value or dis-value. IMHO, it does not bring value for the following reasons:

  1. code gets actually harder to read, and not only in a sense of merely reading it, but also in a sense that you may have hard times when trying to understand what is the actual type you are getting the reference to;
  2. even just for troubleshooting many times it turns out you have to print the actual type to see what is going on;
  3. polymorphic calls are not supported and polymorphism, in general, is not possible, because compiler cannot infer different types with the same code;
  4. code actually gets inconsistent, as you'll be having explicit types for fields and "var"s for local variables. This will also confuse newcomers to the code base;
  5. explicitly declare type in import is actually positive rather than negative: dependencies are clearer and you know what you are using.

My two cents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants