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

Improve weighing between primitive numbers #3

Open
chachako opened this issue Feb 13, 2022 · 0 comments
Open

Improve weighing between primitive numbers #3

chachako opened this issue Feb 13, 2022 · 0 comments
Assignees

Comments

@chachako
Copy link
Contributor

这里需要考虑计算原始类型数字之间转换的重量

/**
* Calculates the weight of the [passed] class relative to the [declared] class.
*
* @return The lighter the weight, the better they match.
* @author 凛 (RinOrz)
*/
internal fun calculateClassWeight(
passed: Class<*>?,
declared: Class<*>,
depth: Boolean
): Float {

if (passed.isPrimitiveNumber && declared.isPrimitiveNumber) return PRIMITIVE_WEIGHT

目前不同数字类型之间的重量都是一致的,这会导致匹配范围会非常宽松

动机

考虑以下案例∶

class Foo {
  constructor(number: Double)
  constructor(number: Float)
  constructor(number: Byte)
}
Foo.new(10)

这个案例传入了一个 int 参数,然而类中不存在 int 参数的构造函数,因此根据目前的重量计算,所有构造函数都会匹配成功。

因此更好的方式应该是匹配数字种类更接近的类型

  • 例如匹配一个 int,只要整数没有超出 byte 的范围,那么 byte 的优先级最高,然后是 bits 相同的 float 再到 longdouble, 以此类推
  • 例如匹配一个 float ,然后有一个 double 参数和 long 参数的构造函数,那么参数为 double 类型的优先
@chachako chachako added this to the API Implementation milestone Feb 13, 2022
@chachako chachako self-assigned this Feb 23, 2022
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

No branches or pull requests

1 participant