简体   繁体   English

Kotlin 冲突声明:val sinAngle: Float, val sinAngle: Float

[英]Kotlin Conflicting declarations: val sinAngle: Float, val sinAngle: Float

I have a data class and function:我有一个数据类和函数:

data class SinCosAngle(val sa: Float, val ca: Float)

fun angleCalc(rad: Float): SinCosAngle {
    return SinCosAngle(sin(rad), cos(rad))
}

I want to use the function multiple times like:我想多次使用该功能,例如:

for (i in 0..72) {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(i * 5f - 90f)) // 1
    (...)
    canvas.drawLine(x1, y1, x2, y2, paint)
}
paint.strokeWidth = 3f
for (i in 0..8) {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(i * 45f - 90f)) // 2
    (...)
    canvas.drawLine(x1, y1, x2, y2, paint)
}

val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f)) // 3
(...)

val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f - angleArrow)) // 4
(...)

val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f + angleArrow)) // 5
(...)

I got errors only at lines 3,4,5.我只在第 3、4、5 行出错。 Lines 1 and 2 are OK.第 1 行和第 2 行没问题。

Conflicting declarations: val sinAngle: Float, val sinAngle: Float

I can fix errors by changing the variable names at lines 3 and 4 but line 5 can remain unchanged.我可以通过更改第 3 行和第 4 行的变量名称来修复错误,但第 5 行可以保持不变。

Working code:工作代码:

for (i in 0..72) {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(i * 5f - 90f))
    x1 = x + cosAngle * radius
    y1 = y + sinAngle * radius
    x2 = x + cosAngle * radiusShort
    y2 = y + sinAngle * radiusShort
    canvas.drawLine(x1, y1, x2, y2, paint)
}
paint.strokeWidth = 3f
for (i in 0..8) {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(i * 45f - 90f))
    x1 = x + cosAngle * radius
    y1 = y + sinAngle * radius
    x2 = x + cosAngle * radiusShort
    y2 = y + sinAngle * radiusShort
    canvas.drawLine(x1, y1, x2, y2, paint)
}
(...)
paint.strokeWidth = 2f
paint.color = getColor(R.color.red)
val (sinAngle1, cosAngle1) = angleCalc(deg2rad(course - 90f))
x2 = x + cosAngle1 * radiusCourse
y2 = y + sinAngle1 * radiusCourse
canvas.drawLine(x, y, x2, y2, paint)
x1 = x + cosAngle1 * radius
y1 = y + sinAngle1 * radius
canvas.drawCircle(x1, y1, smallCircleRadius, paint)
val (sinAngle2, cosAngle2) = angleCalc(deg2rad(course - 90f - angleArrow))
x1 = x2 - cosAngle2 * radiusArrow
y1 = y2 - sinAngle2 * radiusArrow
canvas.drawLine(x1, y1, x2, y2, paint)
val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f + angleArrow))
x1 = x2 - cosAngle * radiusArrow
y1 = y2 - sinAngle * radiusArrow
canvas.drawLine(x1, y1, x2, y2, paint)

How to propertly use the same val name in this case ?在这种情况下如何正确使用相同的 val 名称?

(Android Studio 2020.3.1 Patch 3 October 1, 2021) (Android Studio 2020.3.1 补丁 3 2021 年 10 月 1 日)

EDIT / SOLUTION Thank you for all answers.编辑/解决方案感谢您的所有回答。 I ended up with simple solution:我最终得到了简单的解决方案:

var sc: SinCosAngle

With that I can use angleCalc function without limits and extract fields with sc.sa and sc.ca有了它,我可以无限制地使用angleCalc 函数,并使用 sc.sa 和 sc.ca 提取字段

If you want to still use the same name for the variables, you can introduce a new scope with scope functions.如果您仍想对变量使用相同的名称,您可以引入一个带有作用域函数的新作用域。 For example, putting each declaration in a run block例如,将每个声明放在一个run块中

run {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f))
    // use the angles here
}
run {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f - angleArrow))
    // use the angles here
}
run {
    val (sinAngle, cosAngle) = angleCalc(deg2rad(course - 90f + angleArrow))
    // use the angles here
}

I find let more readable:我发现let更具可读性:

angleCalc(deg2rad(course - 90f)).let { (sinAngle, cosAngle) ->
    // use the angles here
}

angleCalc(deg2rad(course - 90f - angleArrow)).let { (sinAngle, cosAngle) ->
    // use the angles here
}

angleCalc(deg2rad(course - 90f + angleArrow)).let { (sinAngle, cosAngle) ->
    // use the angles here
}

However, you cannot reuse the same property, unless you give up on using the destructuring syntax:但是,您不能重用相同的属性,除非您放弃使用解构语法:

var angles = angleCalc(deg2rad(course - 90f))
// use the angles here

// reusing the variable, without destructuring
angles = angleCalc(deg2rad(course - 90f - angleArrow))

Destructuring only works when you declare a property.解构仅在您声明属性时才有效。 The full name of this feature is called " Destructuring Declarations "这个特性的全称叫做“ Destructuring Declarations

You can't do that.你不能那样做。 You can not have two variables with same name in the same scope.同一个作用域中不能有两个同名的变量。

Lines 1 and 2 are working because those variable are in the scope of for loop.第 1 行和第 2 行正在工作,因为这些变量在for循环的范围内。 Lines 3, 4 and 5 are in the same scope that's why you can't use the same variable name in these lines.第 3、4 和 5 行在相同的范围内,这就是您不能在这些行中使用相同变量名称的原因。

One workaround could be to use a var for the radian angle (similar to how you are using x1 , y1 etc.):一种解决方法可能是使用var作为弧度角(类似于您使用x1y1等的方式):

var angle = deg2rad(course - 90f)
x2 = x + cos(angle) * radiusCourse
y2 = y + sin(angle) * radiusCourse
canvas.drawLine(x, y, x2, y2, paint)

angle = deg2rad(course - 90f - angleArrow)
x1 = x2 - cos(angle) * radiusArrow
y1 = y2 - sin(angle) * radiusArrow
canvas.drawLine(x1, y1, x2, y2, paint)

and so on...

This one does look a little cleaner :)这个看起来确实更干净一点:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM