Home > database >  Kotlin Conflicting declarations: val sinAngle: Float, val sinAngle: Float
Kotlin Conflicting declarations: val sinAngle: Float, val sinAngle: Float

Time:11-13

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. Lines 1 and 2 are OK.

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.

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 ?

(Android Studio 2020.3.1 Patch 3 October 1, 2021)

CodePudding user response:

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 {
    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:

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"

CodePudding user response:

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. Lines 3, 4 and 5 are in the same scope that's why you can't use the same variable name in these lines.

One way could be to use a var for the radian angle (similar to how you are using x1, y1 etc.):

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 :)

  • Related