David Callanan
David Callanan

Reputation: 5968

Declaring constants in Kotlin - calling functions at compile time

For compile-time constants, is there a way of doing

const val myAngle = Math.toRadians(45.0)

without including the Math.toRadians function logic directly in code

const val myAngle = 45.0 / 180.0 * Math.PI

and without doing the calculation beforehand

const val myAngle = Math.PI / 2 // 45.0 degrees

i.e. is there a way of calling Math.toRadians at compile-time?

Edit - My usecase: I am trying to create some angle constants, I would like to input them in degrees, but I would like the stored constant to be in radians.

Upvotes: 1

Views: 517

Answers (1)

Alexey Soshin
Alexey Soshin

Reputation: 17701

No. The reason for that is that there's no guarantee that the return value of any function is a constant (can be computed in compile time).

It's perfectly fine to use val myAngle = Math.toRadians(45.0), though.

Upvotes: 3

Related Questions