Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved reference: pow in Eclipse using Kotlin

I'm trying to write something small in Kotlin and I'm having problems with finding the second power of a Double number.

According to this, Double should implement a pow function receiving another Double, but when I try using this method I get Unresolved reference: pow and an error.

Here's my sample code:

fun main()
{
    val d: Double = 1.1;
    val d2: Double = d.pow(2.0); // Here's the error, on the token 'pow'.
    println(d2);
}

I can't find any reason to this. This feature is only from Kotlin 1.2, but the Kotlin record in the Eclipse Installation Details says Kotlin language support for Kotlin 1.2.50.

I created the project before I updated the Kotlin plugin and it's possible the project was created for Kotlin version before 1.2 but I can't find in the settings anywhere to change the configured Kotlin version so I assume the used version is the one installed i.e. 1.2.50.

By the way, the error icon Eclipse presents is the error with light bulb one, suggesting available solutions exist, but none show up when I click the icon, which is weird.

If anyone could suggest any reason for this, it would be great.
Thanks in advance.

like image 792
Neo Avatar asked Jul 03 '18 08:07

Neo


People also ask

What does unresolved reference mean in Kotlin?

To conclude, the unresolved reference error happens when Kotlin has no idea what the keyword you're typing in the file points to. It may be a function, a variable, or another construct of the language that you've yet to declare in the code.

What does POW () function do in Kotlin?

In this program, we used standard library function Math. pow() to calculate the power of base.


1 Answers

You need to import the function pow into your file:

import kotlin.math.*

My full code:

import kotlin.math.pow

fun main(args: Array<String>)
{
    val d: Double = 1.1;
    val d2: Double = d.pow(2.0); // Here's the error, on the token 'pow'.
    println(d2);
}
like image 81
voddan Avatar answered Nov 15 '22 05:11

voddan