Why did kotlin drop the new keyword ? It makes it harder to see the difference between a function call and an object allocation.
相关问题
- How to refresh height of a Constrained View in Con
- Android Room Fetch data with dynamic table name
- Access Binding Adapters in multi module project
- How to make request-bound data globally available
- In Vertx I need to redirect all HTTP requests to t
相关文章
- SonarQube: How to suppress a warning in Kotlin cod
- What are the `^let` annotations in Android Studio
- Create Custom Dagger 2 Scope with Kotlin
- Android Studio 3.5 ERROR: Unable to resolve depend
- Kotlin inlined extension property
- Kotlin Koans with EduTools plugin: “Failed to laun
- “lateinit” or “by lazy” when defining global andro
- Convert java to kotlin on paste
The Kotlin Coding Conventions clearly state that:
If you follow the above and treat
constructor
as regular function that can be called i.e.val invoice = Invoice()
thenew
keyword becomes redundant. Once you accommodate yourself with the convention it's clear what a code is doing.In fact even in Java code you'll have many implicit allocations that happen just beneath a method call like
Collections.singleton(o)
or Guava'sLists.newArrayList()
so I don't think your argument about allocation visibility being better with thenew
keyword is fully valid.(IMO) It was done because there is NO real difference between functions and object construction, i.e. nothing prevents a function to allocate an object (and they often do).
A good example is factory functions. These functions create new objects, but they are in no way class constructors.
AFAIK, the
new
keyword was created because of a negative experience with C\C++, where functions, returning new objects, have to be specially marked (by name conventions) in order not to forget to (manually) free the memory. In a auto-memory-managing language like Java\Kotlin it is not a concern.Several other languages have no
new
keyword (Python, Scala, maybe Ceylon) and people who have switched to those languages never seem to miss it. I know I dont.