为什么Kotlin放弃了“新"产品?关键词? [英] Why did kotlin drop the "new" keyword?

查看:111
本文介绍了为什么Kotlin放弃了“新"产品?关键词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

kotlin为什么放弃 new 关键字? 这样就很难看到函数调用和对象分配之间的区别.

Why did kotlin drop the new keyword ? It makes it harder to see the difference between a function call and an object allocation.

推荐答案

科林编码规则明确指出:

  • 使用camelCase命名(并避免在名称中使用下划线)
  • 类型以大写字母开头
  • 方法和属性以小写字母开头
  • use of camelCase for names (and avoid underscore in names)
  • types start with upper case
  • methods and properties start with lower case

如果您遵循上述内容并constructor作为常规功能处理,即可以被调用,即val invoice = Invoice() new关键字将变为 redundant (冗余). 一旦适应了约定,就很清楚代码在做什么.

If you follow the above and treat constructor as regular function that can be called i.e. val invoice = Invoice() the new keyword becomes redundant. Once you accommodate yourself with the convention it's clear what a code is doing.

实际上,即使在Java代码中,您也会在 Lists.newArrayList() ,所以我认为您关于使用new关键字提高分配可见性的论点是完全无效的.

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's Lists.newArrayList() so I don't think your argument about allocation visibility being better with the new keyword is fully valid.

这篇关于为什么Kotlin放弃了“新"产品?关键词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆